{"id":406,"date":"2022-04-19T16:08:38","date_gmt":"2022-04-19T08:08:38","guid":{"rendered":"http:\/\/gjweb.top\/?p=406"},"modified":"2022-04-19T16:08:39","modified_gmt":"2022-04-19T08:08:39","slug":"08-compositionapi-%e4%bd%bf%e7%94%a8-vuex","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=406","title":{"rendered":"08.CompositionAPi \u4f7f\u7528 Vuex"},"content":{"rendered":"\n<p>vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;div class=\"about\"&gt;<br> &nbsp; &nbsp;&lt;h1&gt;This is an about page&lt;\/h1&gt;<br> &nbsp; &nbsp;&lt;h2 @click=\"handleClick\"&gt;{{name}}&lt;\/h2&gt;<br> &nbsp;&lt;\/div&gt;<br>&lt;\/template&gt;<br>&lt;script&gt;<br>import { toRefs } from 'vue';<br>import {useStore} from \"vuex\"<br>export default {<br> &nbsp;setup() {<br> &nbsp; &nbsp;const store = useStore();<br> &nbsp; &nbsp;\/\/ const name = store.state.name;<br> &nbsp; &nbsp;const {name} = toRefs(store.state);<br> &nbsp; &nbsp;const handleClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp;store.commit(\"change\",\"123456\")<br> &nbsp;  }<br> &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp;name,<br> &nbsp; &nbsp; &nbsp;handleClick<br> &nbsp;  }<br>  },<br>}<br>&lt;\/script&gt;<br>\u200b<\/code><\/pre>\n\n\n\n<p>store\/index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { createStore } from 'vuex'<br>\/\/ Vuex \u5168\u5c40\u6570\u636e\u7ba1\u7406\u5668<br>\/\/ Vuex \u521b\u5efa\u4e86\u4e00\u4e2a\u5168\u5c40\u552f\u4e00\u7684\u4ed3\u5e93,\u7528\u6765\u5b58\u653e\u5168\u5c40\u7684\u6570\u636e<br>export default createStore({<br> &nbsp;state: {<br> &nbsp; &nbsp;name:\"hello world\"<br>  },<br> &nbsp;\/\/ mutaion \u91cc\u9762\u53ea\u5141\u8bb8\u5199\u540c\u6b65\u4ee3\u7801,\u4e0d\u5141\u8bb8\u5199\u5f02\u6b65\u4ee3\u7801<br> &nbsp;\/\/ commit he mutations \u505a\u5173\u8054<br> &nbsp;mutations: {<br> &nbsp; &nbsp;change(store,str){<br> &nbsp; &nbsp; &nbsp;console.log(store)<br> &nbsp; &nbsp; &nbsp;this.state.name = \"hello \"+str<br> &nbsp;  }<br>  },<br> &nbsp;\/\/ dispatch \u548c cations \u505a\u5173\u8054<br> &nbsp;actions: {<br> &nbsp; &nbsp;\/\/ \u7b2c\u4e8c\u90e8 store \u611f\u77e5\u5230\u4e86\u89e6\u53d1\u4e86\u4e00\u4e2a\u53eb change \u7684 action ,\u6267\u884cchange<br> &nbsp; &nbsp;change(){<br> &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u4e09\u90e8 \u63d0\u4ea4\u4e00\u4e2acommit \u89e6\u53d1\u4e00\u4e2a mutation<br> &nbsp; &nbsp; &nbsp;this.commit('change')<br> &nbsp;  }<br>  },<br> &nbsp;modules: {<br>\u200b<br>  }<br>})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>vue store\/index.js<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-composition"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=406"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/406\/revisions\/407"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}