【发布时间】:2020-09-12 14:41:59
【问题描述】:
我开始使用 Vue。我尝试在其他帖子中找到解决方案,但没有成功。所以:
我有两部分代码:
//main.js
...
const axiosInstance = axios.create({
mode: 'no-cors',
baseURL: process.env.VUE_APP_BASE_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
})
Vue.prototype.$http = axiosInstance
Vue.config.productionTip = false
new Vue({
router,
store,
vuetify,
i18n,
render: h => h(App),
}).$mount('#app')
和
//store.js
export default new Vuex.Store({
...
actions: {
[AUTH_REQUEST]: ({commit, dispatch}, user) => {
return new Promise((resolve, reject) => {
commit(AUTH_REQUEST)
this.$http.post('/auth/login', user)
.then(resp => {...}
})
}
})
当我在组件方法中触发this.$store.dispatch(AUTH_REQUEST, ...) 时,我在控制台中看到错误,例如Uncaught (in promise) TypeError: Cannot read property '$http' of undefined。
我试过this.$http,this._vm.$http - 一样。
【问题讨论】:
-
vuex 操作没有
this的值
标签: javascript vue.js vuex