【发布时间】:2017-09-21 04:48:18
【问题描述】:
我已经在 vue.js 中建立了一个 store,并且可以访问组件计算部分的状态参数:
computed: {
BASE_URL () {
return this.$store.state.BASE_URL;
}
但是,当我尝试在同一组件的方法中访问商店时:
methods: {
register: function () {
axios.post( this.BASE_URL + "/web/register", {
username: this.username,
password: this.password,
email: this.email
}).then(function(data){
this.$store.commit('saveToken', data.token);
console.log('token is set to:', this.$store.state.token)
});
}
},
我在控制台收到此错误:
Uncaught (in promise) TypeError: Cannot read property '$store' of 未定义
我也试过$store 没有this 但得到同样的错误。
这里有什么问题?我该如何解决?
【问题讨论】:
-
你的应用注册vuex了吗?
Vue.use(Vuex) -
是的,我做到了。正如我所说,vuex 可以在组件的
computed部分访问,但不能在方法的 promise 中访问。
标签: javascript vue.js vuex