vue中全局路由守衛(wèi)中替代this操作(this.$store/this.$vux)
全局路由守衛(wèi)this.$vux.loading.hide()報(bào)錯(cuò),訪問不到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判斷防止第一次初始化報(bào)錯(cuò)
或者
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ǔ)充知識(shí):解決導(dǎo)航守衛(wèi)使用不了this.$store
在vue router的導(dǎo)航守衛(wèi)如beforeEach()中是無法直接通過this.$store去操作vuex的,因?yàn)檫@里的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)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis2. Python 忽略文件名編碼的方法3. Java Media Framework 基礎(chǔ)教程4. 解決vue頁面刷新,數(shù)據(jù)丟失的問題5. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作6. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無bug)7. 在Mac中配置Python虛擬環(huán)境過程解析8. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查9. python excel和yaml文件的讀取封裝10. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML
