{"id":1014,"date":"2023-03-12T09:23:49","date_gmt":"2023-03-12T01:23:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1014"},"modified":"2023-04-29T13:16:06","modified_gmt":"2023-04-29T05:16:06","slug":"vuex-obtains-this-object-and-deep-state-monitoring","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vuex-obtains-this-object-and-deep-state-monitoring\/","title":{"rendered":"Vuex\u83b7\u53d6this\u5bf9\u8c61\u53ca\u6df1\u5ea6state\u76d1\u6d4b"},"content":{"rendered":"<p>Vuex\u4e2d\u65e0\u6cd5\u83b7\u53d6\u5230this\u5bf9\u8c61\uff0c\u9700\u8981\u901a\u8fc7dispatch\u4f20\u5165\u4e0a\u4e0b\u6587this<\/p>\n<p>Vue\u76d1\u6d4bstate\u5bf9\u8c61\u5185\u90e8\u5b57\u6bb5\u66f4\u65b0\u6216\u6570\u7ec4\u67d0\u4e00\u6570\u636e\u66f4\u65b0\u53ef\u7528\uff1a<code>deep: true<\/code><\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">computed: mapState({\n  user_update_watcher: state =&gt; state.account.user_update\n}),\nwatch: {\n  user_update_watcher: {\n    handler: &#039;userUpdateWatcher&#039;,\n    deep: true\n  }\n},\nmethods: {\n  userUpdate () {\n    var params = {}\n    params.localMsg = {&#039;msg_server_busy&#039;: &#039;\u7cfb\u7edf\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01&#039;}\n    this.$store.dispatch(&#039;userUpdate&#039;, {&#039;context&#039;: this, &#039;userId&#039;: this.userId, &#039;params&#039;: params})\n  },\n  userUpdateWatcher (data, oldData) {\n    ...\n  }\n}<\/code><\/pre>\n<pre><code class=\"language-javascript\">export default {\n  state: {\n    user_update: {\n      &#039;data&#039;: null,\n      &#039;success&#039;: false,\n      &#039;timestamp&#039;: 0\n    }\n  },\n  mutations: {\n    COMMON_USER_UPDATE_CALLBACK: (state, response) =&gt; {\n      Vue.set(state.user_update, &#039;timestamp&#039;, new Date().getTime())\n      let context = response.context\n      let httpCode = response.httpCode\n      if (httpCode === 403) {\n        Vue.set(state.user_update, &#039;success&#039;, false)\n        context.$router.push({path: &#039;\/login?page=user_update&#039;})\n        return\n      }\n      if (httpCode !== 200) {\n        Vue.set(state.user_update, &#039;success&#039;, false)\n        context.$dialog.alert({mes: response.localMsg.msg_server_busy, btn_mes: &#039;\u786e\u8ba4&#039;})\n        return\n      }\n      let code = response.data.code\n      if (code !== 0) {\n        Vue.set(state.user_update, &#039;data&#039;, null)\n        Vue.set(state.user_update, &#039;success&#039;, false)\n        context.$dialog.alert({mes: response.data.error.errMsg, btn_mes: &#039;\u786e\u8ba4&#039;})\n        return\n      }\n      Vue.set(state.user_update, &#039;data&#039;, response.data.data)\n      Vue.set(state.user_update, &#039;success&#039;, true)\n    }\n  },\n  actions: {\n    userUpdate ({ commit }, object) {\n      \/\/ \u66f4\u65b0\u7528\u6237\u4fe1\u606f\n      api.user_update(object.userId, object.params).then((response) =&gt; {\n        commit(types.COMMON_USER_UPDATE_CALLBACK, {&#039;context&#039;: object.context, &#039;httpCode&#039;: response.status, &#039;data&#039;: response.data})\n      }).catch((response) =&gt; {\n        commit(types.COMMON_USER_UPDATE_CALLBACK, {&#039;context&#039;: object.context, &#039;httpCode&#039;: response.status, &#039;data&#039;: response.data, &#039;localMsg&#039;: object.params.localMsg})\n      })\n    }\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vuex\u4e2d\u65e0\u6cd5\u83b7\u53d6\u5230this\u5bf9\u8c61\uff0c\u9700\u8981\u901a\u8fc7dispatch\u4f20\u5165\u4e0a\u4e0b\u6587this Vue\u76d1\u6d4bstate\u5bf9\u8c61\u5185\u90e8\u5b57\u6bb5 [&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-1014","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\/1014","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=1014"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1014\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}