Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

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)
    }
  }
}
上一篇 Vuex状态监听mapState与普通的局部计算属性混合使用
下一篇 解决Vuex在页面刷新后state数据被清除的问题