Vue.js Vuex操作看不到来自其他模块的getter
我试图使用其他模块的getter将JWT令牌获取到我的http查询中,如下所示:Vue.js Vuex操作看不到来自其他模块的getter,vue.js,vuejs2,axios,jwt,vuex,Vue.js,Vuejs2,Axios,Jwt,Vuex,我试图使用其他模块的getter将JWT令牌获取到我的http查询中,如下所示: ... }, actions: { async refreshContactData( state, getters, rootState, rootGetters ) { return axios.get('/test', { headers:
...
},
actions: {
async refreshContactData( state, getters, rootState, rootGetters ) {
return axios.get('/test', {
headers: {
Authorization: 'Bearer ' + rootGetters['user/getJWT']//the token is a variable which holds the token
}
}).then(response => {
console.log(response)
})
}
},
}
我的第二个模型如下所示:
//user.js
import axios from "axios"
export default {
state: {
jwt: 'asdfasdf',
},
getters: {
getJWT: (state) => {
console.log("WTF")
return state.jwt;
}
},
...
它与main index.js存储文件连接:
//index.js
...
modules: {
user: User,
contact: Contact
},
...
我尝试了不同的配置,但控制台中仍然出现“未定义”错误:
vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'user/getJWT' of undefined
at _callee$ (contact.js?7526:54)
at tryCatch (runtime.js?96cf:63)
at Generator.invoke [as _invoke] (runtime.js?96cf:293)
at Generator.eval [as next] (runtime.js?96cf:118)
at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
at _next (asyncToGenerator.js?1da1:25)
at eval (asyncToGenerator.js?1da1:32)
at new Promise (<anonymous>)
at eval (asyncToGenerator.js?1da1:21)
at Store.refreshContactData (contact.js?7526:47)
vue.runtime.esm.js?2b0e:1888类型错误:无法读取未定义的属性“user/getJWT”
在_callee$(contact.js?7526:54)
在tryCatch(runtime.js?96cf:63)
在Generator.invoke[as_invoke](runtime.js?96cf:293)
在Generator.eval[as next](runtime.js?96cf:118)
在asyncGeneratorStep(asyncToGenerator.js?1da1:3)
at_next(asyncToGenerator.js?1da1:25)
评估时(asyncToGenerator.js?1da1:32)
在新的承诺()
评估时(asyncToGenerator.js?1da1:21)
在Store.refreshContactData(contact.js?7526:47)
我做错了什么?操作不像getter那样有多个参数。它们有一个上下文参数和一个有效负载参数。要获取
rootGetters
,可以分解上下文参数:
async refreshContactData({rootGetters}){//注意大括号
...
}
然后将定义rootgetter