vue中全局路由守衛(wèi)中替代this操作(this.$store/this.$vux)
全局路由守衛(wèi)this.$vux.loading.hide()報錯,訪問不到this
解決辦法
申明變量代替this
main.js文件方法
router.beforeEach((to, from, next) => {
if(vue){
vue.$vux.loading.hide()
}else{
}
next()
})
let vue = new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
if判斷防止第一次初始化報錯
或者
let vue = new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
router.beforeEach((to, from, next) => {
// if(vue){
vue.$vux.loading.hide()
// }else{
// }
next()
})
補(bǔ)充知識:解決導(dǎo)航守衛(wèi)使用不了this.$store
在vue router的導(dǎo)航守衛(wèi)如beforeEach()中是無法直接通過this.$store去操作vuex的,因為這里的this指向不一致。
解決方式是在router的index.js中引入初始化好的store
import store from '@/store'
然后在導(dǎo)航守衛(wèi)中可直接拿到router了
/**導(dǎo)航守衛(wèi) */
router.beforeEach((to, form, next) => {
console.log(store.getters)
})
以上這篇vue中全局路由守衛(wèi)中替代this操作(this.$store/this.$vux)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue修改數(shù)據(jù)的時候,表單值回顯不正確問題及解決
這篇文章主要介紹了vue修改數(shù)據(jù)的時候,表單值回顯不正確的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
詳解vue-cli + webpack 多頁面實例配置優(yōu)化方法
本篇文章主要介紹了詳解vue-cli + webpack 多頁面實例配置優(yōu)化方法,具有一定的參考價值,有興趣的可以了解一下2017-07-07
Vue+Axios實現(xiàn)文件上傳自定義進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Vue+Axios實現(xiàn)文件上傳自定義進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08

