日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

vuex的命名空间有哪些_Vuex在vue路由器中访问命名空间模块的getter

發(fā)布時(shí)間:2023/12/20 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vuex的命名空间有哪些_Vuex在vue路由器中访问命名空间模块的getter 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我試圖通過(guò)檢查用戶是否經(jīng)過(guò)身份驗(yàn)證來(lái)保護(hù)我的路由,這是示例路由:

{

path: '/intranet',

component: search,

meta: { requiresAuth: true },

props: {

tax: 'type',

term: 'intranet-post',

name: 'Intranet'

}

},

我正在這樣設(shè)置警衛(wèi):

router.beforeEach((to, from, next) => {

if (to.matched.some(record => record.meta.requiresAuth)) {

let authenticated = this.$store.getters['auth/getAuthenticated'];

if (!authenticated) {

next({

path: '/login',

query: { redirect: to.fullPath }

})

} else {

next()

}

} else {

next()

}

})

這是auth的vuex模塊:

import Vue from "vue";

export default {

namespaced: true,

state: {

authenticated: !!localStorage.getItem("token"),

token: localStorage.getItem("token")

},

mutations: {

login: function(state, token){

state.authenticated = true;

state.token = token;

},

logout: function(state){

state.authenticated = false;

state.token = '';

}

},

actions: {

login: function({commit}, token){

localStorage.setItem('token', token);

commit('login', token);

},

logout: function({commit}){

localStorage.removeItem("token");

commit('logout');

}

},

getters: {

getToken: (state) => state.token,

getAuthenticated: (state) => state.authenticated,

}

}

但是,當(dāng)我嘗試訪問(wèn)auth getter時(shí),就像路由防護(hù)中顯示的那樣,我收到一個(gè)錯(cuò)誤:

無(wú)法讀取未定義的屬性'getters'

我做錯(cuò)了什么,我該如何解決這個(gè)問(wèn)題?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的vuex的命名空间有哪些_Vuex在vue路由器中访问命名空间模块的getter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。