Vuex Do not mutate vuex store state outside mutation handlers

1
[vuex] Do not mutate vuex store state outside mutation handlers

解决:更改 Vuex 的 store 中的状态的唯一方法是提交 mutation,即通过 dispatch -> actions -> commit -> mutations 更新状态

1
2
3
4
5
6
7
8
9
10
methods: {
setUser () {
let user = {
'username': this.user.username,
'name': this.user.name,
'age': this.user.age
}
this.$store.dispatch('setUser', user)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export default {
state: {
user: {
'data': null,
'state': '',
'timestamp': 0
}
},
mutations: {
COMMON_USER_SET_CALLBACK: (state, user) => {
Vue.set(state.school, 'timestamp', new Date().getTime())
Vue.set(state.user, 'data', user)
}
},
actions: {
setUser ({ commit }, user) {
commit(types.COMMON_SCHOOL_SET_CALLBACK, user)
}
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :