Vuex Do not mutate vuex store state outside mutation handlers

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

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

methods: {
  setUser () {
    let user = {
      'username': this.user.username,
      'name': this.user.name,
      'age': this.user.age
    }
    this.$store.dispatch('setUser', user)
  }
}
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)
    }
  }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/12/vuex-do-not-mutate-vuex-store-state-outside-mutation-handlers/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Vuex Do not mutate vuex store state outside mutation handlers
[vuex] Do not mutate vuex store state outside mutation handlers 解决:更改 Vuex 的 store 中的状态的唯一方法是提交 mutation,即通过 dispatch -> ac……
<<上一篇
下一篇>>
文章目录
关闭
目 录