【发布时间】:2020-07-10 19:46:21
【问题描述】:
在使用vuex 提交新用户后,我尝试将boolean 设置为true,但出现Uncaught (in promise) TypeError: Cannot read property 'data' of undefined 的奇怪错误
这是actionsstate中的create方法
async CREATE_USER({state}) {
await axios.post('api/user', state.user)
commit('SET_CREATED_USER');
},
这是变异
SET_CREATED_USER: (state) => {
state.newUserCreated = true;
console.log('user create? -> ', state.newUserCreated);
}
和 on submit 方法
onSubmit() {
this.$store
.dispatch('CREATE_USER')
.then(() => {
this.inProgress = false;
// navigate to user
this.$router.push('users');
})
.catch( ({ response }) => {
this.inProgress = false;
this.errors = response.data.errors;
console.log('you have an error on creating an user')
});
},
我尝试将.then 添加到CREATE_USER 方法中
axios.post('api/user', state.user).then(
() => commit('SET_CREATED_USER');
)
遇到同样的错误 我也试过了
async CREATE_USER({state}, context) {
await axios.post('api/user', state.user)
context.commit('SET_CREATED_USER');
},
我也尝试删除async,但出现错误
Uncaught (in promise) ReferenceError: commit is not defined
【问题讨论】:
标签: vue.js axios http-post vuex commit