欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中全局路由守衛(wèi)中替代this操作(this.$store/this.$vux)

 更新時間:2020年07月24日 09:24:29   作者:Geek碼農(nóng)  
這篇文章主要介紹了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()
})

補充知識:解決導(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)文章

最新評論