{"id":1022,"date":"2023-03-12T09:32:45","date_gmt":"2023-03-12T01:32:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1022"},"modified":"2023-03-12T09:32:45","modified_gmt":"2023-03-12T01:32:45","slug":"vuex-do-not-mutate-vuex-store-state-outside-mutation-handlers","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vuex-do-not-mutate-vuex-store-state-outside-mutation-handlers\/","title":{"rendered":"Vuex Do not mutate vuex store state outside mutation handlers"},"content":{"rendered":"<pre><code>[vuex] Do not mutate vuex store state outside mutation handlers<\/code><\/pre>\n<p>\u89e3\u51b3\uff1a\u66f4\u6539 Vuex \u7684 store \u4e2d\u7684\u72b6\u6001\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u63d0\u4ea4 mutation\uff0c\u5373\u901a\u8fc7 dispatch -&gt; actions -&gt; commit -&gt; mutations \u66f4\u65b0\u72b6\u6001<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">methods: {\n  setUser () {\n    let user = {\n      &#039;username&#039;: this.user.username,\n      &#039;name&#039;: this.user.name,\n      &#039;age&#039;: this.user.age\n    }\n    this.$store.dispatch(&#039;setUser&#039;, user)\n  }\n}<\/code><\/pre>\n<pre><code class=\"language-javascript\">export default {\n  state: {\n    user: {\n      &#039;data&#039;: null,\n      &#039;state&#039;: &#039;&#039;,\n      &#039;timestamp&#039;: 0\n    }\n  },\n  mutations: {\n    COMMON_USER_SET_CALLBACK: (state, user) =&gt; {\n      Vue.set(state.school, &#039;timestamp&#039;, new Date().getTime())\n      Vue.set(state.user, &#039;data&#039;, user)\n    }\n  },\n  actions: {\n    setUser ({ commit }, user) {\n      commit(types.COMMON_SCHOOL_SET_CALLBACK, user)\n    }\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[vuex] Do not mutate vuex store state outside mutation  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[279],"class_list":["post-1022","post","type-post","status-publish","format-standard","hentry","category-vue","tag-vuex"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1022"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}