{"id":367,"date":"2024-10-24T10:13:42","date_gmt":"2024-10-24T02:13:42","guid":{"rendered":"https:\/\/eve2333.top\/?p=367"},"modified":"2024-10-24T10:13:42","modified_gmt":"2024-10-24T02:13:42","slug":"vue-part-10","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=367","title":{"rendered":"Vue part-10"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">vue-resource<\/h1>\n\n\n\n<p>\u5728vue1.0\u65f6\u4ee3\u8bb2\u7684\u6bd4\u8f83\u591a\uff0c\u662fvue.\u63d2\u4ef6\u5e93\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import vueResource from 'vue-resource'\n\nVue.use(vueResource)<\/code><\/pre>\n\n\n\n<p>\u5728vc\u548cvm\u4e2d\u4f1a\u591a\u51fa\u5982\u4e0bF12\u4ee3\u7801\u5373\uff0c$http:() \u200b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"597\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735343-b0b4b216c755449fbb6ef2a9ae342dff-1024x597.png\" alt=\"\" class=\"wp-image-369\" style=\"width:736px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735343-b0b4b216c755449fbb6ef2a9ae342dff-1024x597.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735343-b0b4b216c755449fbb6ef2a9ae342dff-300x175.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735343-b0b4b216c755449fbb6ef2a9ae342dff-768x448.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735343-b0b4b216c755449fbb6ef2a9ae342dff.png 1234w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4ed6\u7684\u7528\u6cd5\u548c\u8fd4\u56de\u503c\u548caxios\u4e00\u6a21\u4e00\u6837\uff0c\u4f46\u662f\u4e0d\u5e38\u7ef4\u62a4\u4e86<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u63d2\u69fd<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u9ed8\u8ba4\u63d2\u69fd<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"718\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735355-7029dbb31bb24a47b8e62505d9dd7959.png\" alt=\"\" class=\"wp-image-370\" style=\"width:784px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735355-7029dbb31bb24a47b8e62505d9dd7959.png 939w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735355-7029dbb31bb24a47b8e62505d9dd7959-300x229.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735355-7029dbb31bb24a47b8e62505d9dd7959-768x587.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"431\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735363-9149712db6494d3baca7a58a3999ab7d.png\" alt=\"\" class=\"wp-image-371\" style=\"width:783px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735363-9149712db6494d3baca7a58a3999ab7d.png 940w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735363-9149712db6494d3baca7a58a3999ab7d-300x138.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735363-9149712db6494d3baca7a58a3999ab7d-768x352.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>App.vue\u7684Category\u6807\u7b7e\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a\u5728Category.vue\u7684\uff1f\uff1f\uff1f\uff1f\u7a76\u7adf\u653e\u5728\u54eavue\u4e0d\u77e5\u9053\uff1b\u56e0\u6b64\u901a\u8fc7slot\u6765\u8868\u660e\u4f4d\u7f6e<\/p>\n\n\n\n<p>App.vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n   &lt;div class=\"container\">\n     &lt;Category title=\"\u7f8e\u98df\">\n       &lt;img src=\"\" alt=\"delicious food\"\/>\n     &lt;\/Category>\n\n     &lt;Category title=\"\u6e38\u620f\" :listData=\"games\">\n       &lt;ul>\n         &lt;li v-for=\"(g , index) in games\" :key=\"index\">{{ g }}&lt;\/li>\n       &lt;\/ul>\n     &lt;\/Category>\n\n     &lt;Category title=\"\u7535\u5f71\">\n       &lt;video src=\"http:\/\/clips.vorwaerts-gmbh.de\/big_buck_bunny.mp4\" controls>&lt;\/video>\n     &lt;\/Category>\n   &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport Category from \"@\/components\/Category\";\nexport default {\n  name: \"App\",\n  components:{\n    Category\n  },\n  data(){\n    return {\n      foods:&#91;'\u706b\u9505','\u70e7\u70e4','\u5c0f\u9f99\u867e','\u725b\u6392'],\n      games:&#91;'\u7ea2\u8272\u8b66\u6212','\u7a7f\u8d8a\u706b\u7ebf','\u52b2\u821e\u56e2','\u8d85\u7ea7\u739b\u4e3d'],\n      films:&#91;'\u300a\u6559\u7236\u300b','\u300a\u62c6\u5f39\u4e13\u5bb6\u300b','\u300a\u4f60\u597d\uff0c\u674e\u7115\u82f1\u300b','\u300a\u5c1a\u7845\u8c37\u300b']\n    }\n  }\n}\n&lt;\/script>\n&lt;style lang=\"css\" scoped>\n   .container{\n     display: flex;\n     justify-content: space-around;\n   }\n   video {\n     width: 100%;\n   }\n   img{\n     width: 100%;\n   }\n&lt;\/style><\/code><\/pre>\n\n\n\n<p>components\u6587\u4ef6\u5939\u7684 category.vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div class=\"category\">\n    &lt;h3>{{ title }}&lt;\/h3>\n    &lt;!--\u63d2\u69fd,\u7b49\u7740\u7ec4\u4ef6\u7684\u4f7f\u7528\u8005\u8fdb\u884c\u586b\u5145-->\n    &lt;slot>\u6211\u662f\u9ed8\u8ba4\u503c\uff0c\u5f53\u4f7f\u7528\u8005\u6ca1\u6709\u4f20\u9012\u5177\u4f53\u7ed3\u6784\u65f6\uff0c\u6211\u4f1a\u51fa\u73b0&lt;\/slot>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nexport default {\n  name: \"Category\",\n  props:&#91;'title' ]\n}\n&lt;\/script>\n\n&lt;style scoped>\n   .category{\n     background: skyblue;\n     width: 200px;\n     height: 300px;\n   }\n   h3{\n     text-align: center;\n     background: orange;\n   }\n   img{\n     width: 100%;\n   }\n&lt;\/style><\/code><\/pre>\n\n\n\n<p>\u6700\u57fa\u7840\u7684main.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5f15\u5165Vue\nimport Vue from \"vue\";\n\/\/\u5f15\u5165vue-resource\n\/\/\u5f15\u5165App\nimport App from '.\/App';\n\n\/\/\u5173\u95edVue\u7684\u751f\u4ea7\u63d0\u793a\nVue.config.productionTip = false;\n\n\nnew Vue({\n    el: '#app',\n    render: h => h(App),\n    beforeCreate() {\n        Vue.prototype.$bus = this;\n    }\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5177\u540d\u63d2\u69fd<\/h3>\n\n\n\n<p>\u5982\u679c\u6709\u4e00\u5929\u51fa\u73b0\u591a\u4e2a\u63d2\u69fd\uff0c\u5c31\u9700\u8981\u591a\u4e2a\u540d\u5b57\u6765\u8054\u5408\u4f7f\u7528 \u200b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"265\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735448-73bb8f43ec984034ac43babe28a90303.png\" alt=\"\" class=\"wp-image-372\" style=\"width:770px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735448-73bb8f43ec984034ac43babe28a90303.png 847w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735448-73bb8f43ec984034ac43babe28a90303-300x94.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735448-73bb8f43ec984034ac43babe28a90303-768x240.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u770b\u89c1\u4e24\u4e2a\u63d2\u69fd\uff0cvue\u9ed8\u8ba4\u7ed9\u4e86double\u53cc\u4efd\uff0c\u56e0\u6b64\u5fc5\u987b\u7ed9\u5404\u81ea\u7684name\uff0c<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u5411\u63d2\u69fd\u91cc\u9762\u653e\u4e1c\u897f\uff0c\u5c31\u4f1a\u51fa\u73b0slot\u91cc\u9762\u7684\u6587\u5b57\uff0c\u56e0\u6b64App.vue\u5c31\u9700\u8981slot<\/p>\n\n\n\n<p><s><strong>slot\u73b0\u5728\u5f03\u7528\u4e86 \u7528v-slot \u5e76\u4e14\u53ea\u80fd\u5728template\u91cc\u4f7f\u7528 \u6216\u8005v-slot=\"name\"\u7b80\u5199\u4e3a#name<\/strong><\/s><\/p>\n\n\n\n<p>\u6b64\u8bdd\u5b58\u7591\uff0c\u56e0\u4e3a\u5728vue\u5b98\u7f51\u4e0a\u4ecd\u7136\u53ef\u4ee5\u641c\u7d22\u5230\u3002\u597d\u5427\uff0c\u4e0a\u8ff0\u66f4\u65b0\u662f2.6\u63d0\u51fa\u7684\u7248\u672c\u66f4\u65b0<\/p>\n\n\n\n<p>App.vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n   &lt;div class=\"container\">\n     &lt;Category title=\"\u7f8e\u98df\">\n       &lt;img slot=\"center\" src=\"\" alt=\"delicious food\"\/>\n       &lt;a href=\"https:\/\/www.baidu.com\" slot=\"footer\">\u767e\u5ea6&lt;\/a>\n     &lt;\/Category>\n\n     &lt;Category title=\"\u6e38\u620f\" :listData=\"games\">\n       &lt;ul slot=\"center\">\n         &lt;li v-for=\"(g , index) in games\" :key=\"index\">{{ g }}&lt;\/li>\n       &lt;\/ul>\n       &lt;div slot=\"footer\" class=\"foot\">\n         &lt;a href=\"https:\/\/www.baidu.com\">\u5355\u673a\u6e38\u620f&lt;\/a>\n         &lt;a href=\"https:\/\/www.baidu.com\">\u7f51\u7edc\u6e38\u620f&lt;\/a>\n       &lt;\/div>\n     &lt;\/Category>\n\n     &lt;Category title=\"\u7535\u5f71\">\n       &lt;video slot=\"center\" src=\"http:\/\/clips.vorwaerts-gmbh.de\/big_buck_bunny.mp4\" controls>&lt;\/video>\n       &lt;!--\u4f46\u662f\u6ce8\u610fv-slot\u4ec5\u4ec5\u53ea\u80fd\u88ab\u7528\u5728\u7ec4\u4ef6\u4e0a\u6216\u8005template\u6807\u7b7e\u4e0a-->\n       &lt;template v-slot:footer>\n         &lt;div class=\"foot\">\n           &lt;a href=\"https:\/\/www.baidu.com\">\u7ecf\u5178&lt;\/a>\n           &lt;a href=\"https:\/\/www.baidu.com\">\u70ed\u95e8&lt;\/a>\n           &lt;a href=\"https:\/\/www.baidu.com\">\u63a8\u8350&lt;\/a>\n         &lt;\/div>\n         &lt;h4>\u6b22\u8fce\u6765\u5230\u5f71\u9662\u89c2\u8d4f&lt;\/h4>\n       &lt;\/template>\n     &lt;\/Category>\n   &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport Category from \"@\/components\/Category\";\nexport default {\n  name: \"App\",\n  components:{\n    Category\n  },\n  data(){\n    return {\n      foods:&#91;'\u706b\u9505','\u70e7\u70e4','\u5c0f\u9f99\u867e','\u725b\u6392'],\n      games:&#91;'\u7ea2\u8272\u8b66\u6212','\u7a7f\u8d8a\u706b\u7ebf','\u52b2\u821e\u56e2','\u8d85\u7ea7\u739b\u4e3d'],\n      films:&#91;'\u300a\u6559\u7236\u300b','\u300a\u62c6\u5f39\u4e13\u5bb6\u300b','\u300a\u4f60\u597d\uff0c\u674e\u7115\u82f1\u300b','\u300a\u5c1a\u7845\u8c37\u300b']\n    }\n  }\n}\n&lt;\/script>\n&lt;style lang=\"css\" scoped>\n   .container, .foot{\n     display: flex;\n     justify-content: space-around;\n   }\n   h4{\n     text-align: center;\n   }\n&lt;\/style><\/code><\/pre>\n\n\n\n<p>main.js\u65e0\u53d8\u5316\uff0cCategory.vue\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div class=\"category\">\n    &lt;h3>{{ title }}&lt;\/h3>\n    &lt;!--\u63d2\u69fd,\u7b49\u7740\u7ec4\u4ef6\u7684\u4f7f\u7528\u8005\u8fdb\u884c\u586b\u5145-->\n    &lt;slot name=\"center\">\u6211\u662f\u9ed8\u8ba4\u503c&lt;\/slot>\n    &lt;slot name=\"footer\">\u6211\u662f\u9ed8\u8ba4\u503c&lt;\/slot>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nexport default {\n  name: \"Category\",\n  props:&#91; 'listData', 'title' ]\n}\n&lt;\/script>\n\n&lt;style scoped>\n   .category{\n     background: skyblue;\n     width: 200px;\n     height: 300px;\n   }\n   h3{\n     text-align: center;\n     background: orange;\n   }\n   img{\n     width: 100%;\n   }\n   video{\n     width: 100%;\n   }\n&lt;\/style><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f5c\u7528\u57df\u63d2\u69fd<\/h3>\n\n\n\n<p>App\u7ec4\u4ef6\u662fCategory\u7ec4\u4ef6\u7684\u4f7f\u7528\u8005\uff0cCategory\u7684\u6570\u636e\u5373data\uff0c\u5f53\u6570\u636e\u653e\u5728App\u91cc\u9762\uff0c<\/p>\n\n\n\n<p><a href=\"https:\/\/www.bilibili.com\/video\/BV1Zy4y1K7SH\/?p=104&amp;spm_id_from=pageDriver&amp;vd_source=b31958e33027799323b8b7d77ae181d4\" target=\"_blank\"  rel=\"nofollow\" >104_\u5c1a\u7845\u8c37Vue\u6280\u672f_\u4f5c\u7528\u57df\u63d2\u69fd_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">vuex<\/h1>\n\n\n\n<p>1.\u6982\u5ff5\uff1a\u4e13\u95e8\u5728Vue\u4e2d\u5b9e\u73b0\u96c6\u4e2d\u5f0f\u72b6\u6001\uff08\u6570\u636e\uff09\u7ba1\u7406\u7684\u4e00\u4e2aVue\u63d2\u4ef6\uff0c\u5bf9vue\u5e94\u7528\u4e2d\u591a\u4e2a\u7ec4\u4ef6\u7684\u5171\u4eab\u72b6\u6001\u8fdb\u884c\u96c6\u4e2d\u5f0f\u7684\u7ba1\u7406\uff08\u8bfb\/\u5199\uff09\uff0c\u4e5f\u662f\u4e00\u79cd\u7ec4\u4ef6\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f\uff0c\u4e14\u9002\u7528\u4e8e\u4efb\u610f\u7ec4\u4ef6\u95f4\u901a\u4fe1\u3002<br>2.Github\u5730\u5740\uff1ahttps:\/\/github.com\/vuejs\/vuex \u200b<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"711\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735499-d55c9049cefa4669a369bacbc1a1acc7.png\" alt=\"\" class=\"wp-image-373\" style=\"width:667px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735499-d55c9049cefa4669a369bacbc1a1acc7.png 948w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735499-d55c9049cefa4669a369bacbc1a1acc7-300x225.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735499-d55c9049cefa4669a369bacbc1a1acc7-768x576.png 768w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" \/><\/figure>\n<\/div>\n\n\n<p>\u5355\u7eaf\u7684\u4f7f\u7528\u5168\u5c40\u4e8b\u4ef6\u603b\u7ebf\u5bfc\u81f4\u7ebf\u592a\u591a\u4e86\uff0c\u5728\u8f83\u591a\u7ec4\u4ef6\u5bb9\u6613\u6df7\u6dc6\u54ce<\/p>\n\n\n\n<p>&nbsp;\u56e0\u6b64\u6620\u5165\u4e86Vuex\uff0c<\/p>\n\n\n\n<p>1.\u591a\u4e2a\u7ec4\u4ef6\u4f9d\u8d56\u4e8e\u540c\u4e00\u72b6\u6001<br>2.\u6765\u81ea\u4e0d\u540c\u7ec4\u4ef6\u7684\u884c\u4e3a\u9700\u8981\u53d8\u66f4\u540c\u4e00\u72b6\u6001\uff0c\u603b\u7ed3\u5c31\u662f\u4e00\u4e2a\"<strong>\u5171\u4eab<\/strong>\"<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"710\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735554-\u5c4f\u5e55\u622a\u56fe-2024-10-24-100534-1024x710.png\" alt=\"\" class=\"wp-image-374\" style=\"width:692px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735554-\u5c4f\u5e55\u622a\u56fe-2024-10-24-100534-1024x710.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735554-\u5c4f\u5e55\u622a\u56fe-2024-10-24-100534-300x208.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735554-\u5c4f\u5e55\u622a\u56fe-2024-10-24-100534-768x532.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735554-\u5c4f\u5e55\u622a\u56fe-2024-10-24-100534.png 1176w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">&nbsp;\u5c0f\u6848\u4f8b<\/h3>\n\n\n\n<p><strong>\u00a0Count.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n    &lt;div&gt;\n        &lt;h1&gt;\u5f53\u524d\u6c42\u548c\u4e3a\uff1a{{ sum }}&lt;\/h1&gt;\n        &lt;select v-model.number=\"n\"&gt;\n            &lt;option value=\"1\"&gt;1&lt;\/option&gt;\n            &lt;option value=\"2\"&gt;2&lt;\/option&gt;\n            &lt;option value=\"3\"&gt;3&lt;\/option&gt;\n        &lt;\/select&gt;\n        &lt;button @click=\"increment\"&gt;+&lt;\/button&gt;\n        &lt;button @click=\"decrement\"&gt;-&lt;\/button&gt;\n        &lt;button @click=\"incrementOdd\"&gt;\u5f53\u524d\u6c42\u548c\u4e3a\u5947\u6570\u518d\u52a0&lt;\/button&gt;\n        &lt;button @click=\"incrementWait\"&gt;\u7b49\u4e00\u7b49\u518d\u52a0&lt;\/button&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n&lt;script&gt;\nexport default {\n    name: 'Count',\n    data() {\n        return {\n            n: 1,\/\/ \u7528\u6237\u9009\u62e9\u7684\u6570\u5b57\n            sum: 0 \/\/\u5f53\u524d\u7684\u548c\n        }\n    },\n    methods:{\n        \/\/\u53ef\u4ee5\u6574\u5408\u4f46\u6ca1\u5fc5\u8981\uff0c\u771f\u5b9e\u5f00\u53d1\u4e2d\u540e\u671f\u4e0d\u65ad\u52a0\u65b0\u9700\u6c42\u4e00\u4e2a\u65b9\u6cd5\u4f1a\u53d8\u5f97\u5f88\u4e71\n        increment() {\n            this.sum += this.n;\n        },\/\/\u5bfc\u81f4\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u5bfc\u81f4\u7c7b\u578b\u9519\u8bef.\u5c31\u662f13+2=132\n        \/\/\u56e0\u6b64\u5728option\u7528\u5192\u53f7\uff0c\u76f4\u63a5\u8ba4\u4e3a\u6210JS\u8868\u8fbe\u5f0f\uff0c\u6570\u5b57\u89e3\u6790\n        \/\/\u6216\u8005v-model.number\n        decrement() {\n            this.sum -= this.n;\n        },\n        incrementOdd() {\n            if(this.sum % 2 === 0) {\n                this.sum += this.n;\n            }\n        },\n        incrementWait() {\n            setTimeout(() =&gt; {\n                this.sum += this.n;\n            }, 1000);\n        }\n    }\n}\n&lt;\/script&gt;\n&lt;style lang=\"css\"&gt;<\/code><\/pre>\n\n\n\n<p><strong>App.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n   &lt;div&gt;\n     &lt;Count\/&gt;\n   &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport Count from \"@\/components\/Count\";\nexport default {\n  name: \"App\",\n  components:{\n    Count\n  },\n}\n&lt;\/script&gt;\n&lt;style lang=\"css\" scoped&gt;\n\n&lt;\/style&gt;<\/code><\/pre>\n\n\n\n<p><strong>main.js\u4ecd\u7136\u4fdd\u6301\u76f8\u540c<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Vuex\u5de5\u4f5c\u539f\u7406<\/h3>\n\n\n\n<p>\u7b80\u5355\u8bf4\uff1aMutations \u80fd\u76f4\u63a5\u64cd\u4f5c State\uff0c\u4f46\u4e0d\u80fd\u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\uff1b\u800c Actions \u53ef\u4ee5\u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f46\u4e0d\u80fd\u76f4\u63a5\u64cd\u4f5c State\uff0c\u6240\u4ee5\u5f02\u6b65\u540e\u901a\u8fc7 Mutations \u4fee\u6539 State&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u8fd9\u4e2aactions\u4e3b\u8981\u5904\u7406mounted\uff0c\u5411\u670d\u52a1\u5668\u53d1\u5f02\u6b65\u8bf7\u6c42\uff0c\u7136\u540e\u83b7\u53d6\u6570\u636e\uff0c\u4fee\u6539\uff0c\u4fdd\u5b58\u5230state\u4ed3\u5e93\u4e2d\uff0c\u7136\u540e\u901a\u8fc7mapState\u7ec4\u4ef6\u80fd\u62ff\u5230\uff0c\u7136\u540e\u8fdb\u884c\u904d\u5386\u6570\u7ec4\u52a8\u6001\u5c55\u793a\u6570\u636e\uff0c\u8fd9\u5c31\u662f\u524d\u53f0\u9879\u76ee\u7684\u5927\u90e8\u5206\u529f\u80fd \u200b<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"642\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735635-8bbb583f55bb4015ad9c0f12c62c866e-1024x642.png\" alt=\"\" class=\"wp-image-375\" style=\"width:640px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735635-8bbb583f55bb4015ad9c0f12c62c866e-1024x642.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735635-8bbb583f55bb4015ad9c0f12c62c866e-300x188.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735635-8bbb583f55bb4015ad9c0f12c62c866e-768x482.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735635-8bbb583f55bb4015ad9c0f12c62c866e.png 1065w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>store\u6765\u7ba1\u7406vuex\u90a3\u4e09\u90e8\u5206\uff0cstore.commit()<\/p>\n\n\n\n<p>vue2\u4e2d\u8981\u7528vuex\u76843\u7248\u672c\uff0cvue3\u4e2dvuex\u76844\u7248\u672c\uff0c\u6240\u4ee5\u8981\u5b89\u88c5vuex3\u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm i vuex@3<\/code><\/pre>\n\n\n\n<p><strong>main.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5f15\u5165Vue\nimport Vue from 'vue' \/\/App\nimport App from '.\/App. vue' \/\/\u5f15\u5165\u63d2\u4ef6\nimport vueResource from 'vue-resource' \/\/\u5f15\u5165vuex\nimport Vuex from 'vuex' \n\n\/\/\u5173\u95edVue\u7684\u751f\u4ea7\u63d0\u793a\nVue. config. productionTip false \n\/\/\u4f7f\u7528\u63d2\u4ef6\nVue.use(vueResource) \nVue.use(Vuex)\n\n\/\/\u521b\u5efavm \nnew Vue({ \n    \/\/\u53ef\u4ee5\u4f20\u5165store\u914d\u7f6e\u9879\uff0cvc\uff0cvm\u5747\u53ef\n    el:'#app',\n    render: h = h(App), \n    beforeCreate(){\n    Vue.prototype.$bus =this \n    }\n}) <\/code><\/pre>\n\n\n\n<p>\u6709\u4e24\u79cd\u9009\u62e9\uff1a<\/p>\n\n\n\n<p>1.\u5728\u9879\u76ee\u6587\u4ef6\u5939\u4e2d\u65b0\u5efavuex\u6587\u4ef6\u5939\uff0c\u91cc\u9762store.js&nbsp; &nbsp; &nbsp; 2.\u65b0\u5efa\u7684store\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u6709index.js\uff08\u5b98\u7f51\u5199\u6cd5\uff09<br>index.js&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u811a\u624b\u67b6\u89e3\u6790import\u89c4\u5219\uff1a..........................................................................................<\/p>\n\n\n\n<p>\u5f00\u53d1\u73af\u5883\u642d\u5efa\u662fimport\u8bed\u53e5\u987a\u5e8f\u8c03\u6362\uff0c\u6587\u4ef6\u6539\u5199\u7b49\u7b49<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6c42\u548c\u6848\u4f8bvuex\u7248<\/h3>\n\n\n\n<p><strong>main.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5f15\u5165Vue\nimport Vue from \"vue\";\n\/\/\u5f15\u5165App\nimport App from '.\/App';\n\/\/\u5f15\u5165store\nimport store from '.\/store';\n\n\/\/\u5173\u95edVue\u7684\u751f\u4ea7\u63d0\u793a\nVue.config.productionTip = false;\n\nnew Vue({\n    el: '#app',\n    store,\n    render: h =&gt; h(App),\n});<\/code><\/pre>\n\n\n\n<p><strong>store\u7684index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u8be5\u6587\u4ef6\u7528\u4e8e\u521b\u5efavuex\u4e2d\u6700\u6838\u5fc3\u7684store\n *\/\n\n\/\/\u5f15\u5165Vuex\nimport Vuex from 'vuex';\nimport Vue from \"vue\";\n\n\/\/\u4f7f\u7528vuex\u6765\u96c6\u4e2d\u7ba1\u7406\u72b6\u6001,\u5fc5\u8981\n\/\/new store\u7684\u524d\u63d0\u662f\u5fc5\u987b\u8981\u4f7f\u7528Vuex\u63d2\u4ef6\nVue.use(Vuex);\n\n\/\/\u521b\u5efaactions(\u672c\u8d28\u5c31\u662f\u5bf9\u8c61) \u7528\u4e8e\u54cd\u5e94\u7ec4\u4ef6\u4e2d\u7684\u52a8\u4f5c\nconst actions = {\n    \/\/\u6536\u5230\u4e0a\u4e0b\u6587\u5bf9\u8c61(\u5305\u542bcommit)\u548cdispatch\u8fc7\u6765\u7684\u503c\n    \/\/ increment(context, value){\n    \/\/     context.commit('INCREMENT', value);\n    \/\/ },\n    \/\/ decrement(context, value){\n    \/\/     context.commit('DECREMENT', value);\n    \/\/ },\n    incrementIfOdd(context, value) {\n        \/\/ console.log(`action\u4e2d\u7684incrementIfOdd\u88ab\u8c03\u7528`);\n        \/\/ console.log('\u5904\u7406\u4e86\u4e00\u4e9b\u4e8b\u60c5');\n        \/\/ context.dispatch('demo1', value);\n        if (context.state.sum % 2) {\n            console.log('@')\n            context.commit('INCREMENT', value);\n            \/\/ context.state.sum += 1;\/\/\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u4f46\u662f\u8bb0\u4f4f\u672c\u6b21\u5bf9\u72b6\u6001\u7684\u6539\u53d8\u5f00\u53d1\u8005\u5de5\u5177\u5c06\u65e0\u6cd5\u6355\u83b7\uff0c\u56e0\u4e3a\u5f00\u53d1\u8005\u5de5\u5177\u662f\u5bf9mutations\u5bf9\u8bdd\u7684\n        }\n    },\n    incrementWait(context, value) {\n        setTimeout(() =&gt; {\n            context.commit('INCREMENT', value);\n        }, 500)\n    },\n    \/\/ demo1(context, value){\n    \/\/     console.log('\u5904\u7406\u4e86\u4e00\u4e9b\u4e8b\u60c5---demo1');\n    \/\/     context.dispatch('demo2', value);\n    \/\/ },\n    \/\/ demo2(context, value){\n    \/\/     console.log('\u5728action\u7684demo\u4e2d\u5b8c\u6210\u6700\u7ec8\u7684\u903b\u8f91');\n    \/\/     if(context.state.sum % 2) {\n    \/\/         console.log('@')\n    \/\/         context.commit('INCREMENT',value);\n    \/\/     }\n    \/\/ }\n}\n\n\/\/\u521b\u5efamutations(\u672c\u8d28\u4e5f\u662f\u5bf9\u8c61) \u7528\u4e8e\u4fee\u6539\u6570\u636e(state)\nconst mutations = {\n    \/\/\u6536\u5230state\u548c\u8981\u64cd\u4f5c\u6570value\n    INCREMENT(state, value) {\n        state.sum += value;\n    },\n    DECREMENT(state, value) {\n        state.sum -= value;\n    },\n}\n\n\/\/\u51c6\u5907getters\u7528\u4e8e\u52a0\u5de5state\uff0c\u5c06\u5176\u5171\u4eab\u4e8e\u5404\u4e2a\u7ec4\u4ef6\u5f53\u4e2d\nconst getters = {\n    bigSum(state) {\n        return state.sum * 10;\n    }\n}\n\n\/\/\u51c6\u5907state(\u6570\u636e) \u5b58\u50a8\u6570\u636e\n\/\/\u7c7b\u4f3c\u4e8e\u5404\u4e2a\u7ec4\u4ef6\u91cc\u7684computed(\u8ba1\u7b97\u5c5e\u6027),\u53ea\u4e0d\u8fc7\u5b83\u662f\u5171\u4eab\u7684\nconst state = {\n    sum: 0,\n    school: 'Wust',\n    subject: 'Computer Science',\n}\n\n\n\/\/\u521b\u5efa\u5e76\u66b4\u9732store\nexport default new Vuex.Store({\n    actions: actions,\n    mutations: mutations,\n    state: state,\n    getters: getters\n});<\/code><\/pre>\n\n\n\n<p><strong>App.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n   &lt;div&gt;\n     &lt;Count\/&gt;\n   &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport Count from \"@\/components\/Count\";\nexport default {\n  name: \"App\",\n  components:{\n    Count\n  },\n  mounted() {\n    console.log('App', this);\n  }\n}\n&lt;\/script&gt;\n&lt;style lang=\"css\" scoped&gt;\n\n&lt;\/style&gt;<\/code><\/pre>\n\n\n\n<p><strong>components\u7684Count.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n  &lt;div&gt;\n    &lt;h1&gt;\u5f53\u524d\u6c42\u548c\u4e3a: {{ sum }}&lt;\/h1&gt;\n    &lt;!--\u8ba9\u5176\u6536\u96c6\u5230\u7684\u6570\u636e\u5168\u662fnumber\u7c7b\u578b\u7684 number\u4fee\u9970\u7b26--&gt;\n    &lt;h3&gt;\u5f53\u524d\u6c42\u548c\u653e\u59273\u500d\u4e3a:{{ bigSum }}&lt;\/h3&gt;\n    &lt;h3&gt;\u6211\u5728{{ school }}, \u5b66\u4e60{{ subject }}&lt;\/h3&gt;\n    &lt;select v-model.number=\"n\"&gt;\n      &lt;!--\u8ba9\u6240\u6709\u7684value\u5168\u90e8\u7ed1\u5b9a\u4e3a\u6570\u5b57--&gt;\n      &lt;option value=\"1\"&gt;1&lt;\/option&gt;\n      &lt;option value=\"2\"&gt;2&lt;\/option&gt;\n      &lt;option value=\"3\"&gt;3&lt;\/option&gt;\n    &lt;\/select&gt;\n    &lt;button @click=\"increment(n)\"&gt;+&lt;\/button&gt;\n    &lt;button @click=\"decrement(n)\"&gt;-&lt;\/button&gt;\n    &lt;button @click=\"incrementIfOdd(n)\"&gt;\u5f53\u524d\u6c42\u548c\u4e3a\u5947\u6570\u518d\u52a0&lt;\/button&gt;\n    &lt;button @click=\"incrementWait(n)\"&gt;\u7b49\u4e00\u7b49\u518d\u52a0&lt;\/button&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport { mapState, mapGetters, mapMutations, mapActions} from 'vuex';\nexport default {\n  \/\/\u8ba1\u6570\u7ec4\u4ef6\n  name: \"Count\",\n  data(){\n    return {\n      n: 1,\/\/ \u4ee3\u8868\u7528\u6237\u5728select\u6846\u5f00\u59cb\u7684\u65f6\u5019\u9009\u62e9\u7684\u6570\u5b57\n    }\n  },\n  computed:{\n    \/\/\u501f\u52a9mapState\u4ecestate\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ... mapState({\n    \/\/   sum:'sum',\n    \/\/   school: 'school',\n    \/\/   subject: 'subject'\n    \/\/ }),\n    \/\/\u501f\u52a9mapState\u4ecestate\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u6570\u7ec4\u5199\u6cd5(\u5373\u4ee3\u8868\u4e86\u751f\u6210\u7684\u8ba1\u7b97\u5c5e\u6027\u540d\u4e3asum\uff0c\u540c\u65f6\u4e5f\u4ee3\u8868\u4e86\u4ecestate\u627e\u5230sum)\n    ... mapState(&#91;'sum', 'school', 'subject']),\n\n    \/\/\u501f\u52a9mapGetters\u4ecegetters\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ...mapGetters({ bigSum: 'bigSum' }),\n    \/\/\u501f\u52a9mapGetters\u4ecegetters\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u6570\u7ec4\u5199\u6cd5\n    ...mapGetters(&#91;'bigSum']),\n\n  },\n  methods:{\n    \/\/ increment(){\n    \/\/   this.$store.commit('INCREMENT', this.n);\n    \/\/ },\n    \/\/ decrement(){\n    \/\/   this.$store.commit('DECREMENT', this.n);\n    \/\/ },\n    \/\/\u501f\u52a9mapMutations\u751f\u6210\u5bf9\u5e94\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4f1a\u8c03\u7528commit\u53bb\u8054\u7cfbmutations\uff0c\u5bf9\u8c61\u5199\u6cd5\n    ...mapMutations({\n      increment: 'INCREMENT',\n      decrement: 'DECREMENT',\n    }),\n    \/\/\u501f\u52a9\u6570\u7ec4\u5199\u6cd5\u751f\u6210\u65b9\u6cd5,\u4f46\u6ce8\u610f\u4f60\u751f\u6210\u7684\u65b9\u6cd5\u540d\u548cmutations\u91cc\u5bf9\u5e94\u7684\u65b9\u6cd5\u540d\u5c06\u4f1a\u4e00\u6837\u7684\n    \/\/ ...mapMutations(&#91;'increment', 'decrement']),\n\n    \/\/ incrementOdd(){\n    \/\/   this.$store.dispatch('incrementIfOdd', this.n);\n    \/\/ },\n    \/\/ incrementWait(){\n    \/\/   this.$store.dispatch('incrementWait', this.n);\n    \/\/ }\n\n    \/\/\u501f\u52a9mapMutations\u751f\u6210\u5bf9\u5e94\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4f1a\u8c03\u7528dispatch\u53bb\u8054\u7cfbactions\uff0c\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ...mapActions({\n    \/\/   incrementIfOdd: 'incrementIfOdd',\n    \/\/   incrementWait: 'incrementWait',\n    \/\/ }),\n\n    ...mapActions(&#91;'incrementWait', 'incrementIfOdd']), \/\/\u6570\u7ec4\u5199\u6cd5,\u540c\u4e0a\n  },\n}\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n   button{\n     margin-left: 5px;\n   }\n&lt;\/style&gt;<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u672c\u6765\u5c31\u662f\u4e3a\u4e2d\u5927\u578b\u9879\u76ee\u8bbe\u8ba1\u7684\uff0c\u5c0f\u9879\u76eevue\u9875\u4e0d\u63a8\u8350<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">vuex\u5f00\u53d1\u8005\u5de5\u5177<\/h3>\n\n\n\n<p>\u548cvue\u5de5\u5177\u540c\u5c5e\u4e00\u5957\uff0c\u5728\u754c\u9762\u4e0a\u7684action\u90fd\u4f1a\u663e\u793a\u4e86 \u200b<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1270\" height=\"738\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735729-20f7a15466cf44d8b8c831b7579d4977.png\" alt=\"\" class=\"wp-image-376\" style=\"width:632px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735729-20f7a15466cf44d8b8c831b7579d4977.png 1270w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735729-20f7a15466cf44d8b8c831b7579d4977-300x174.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735729-20f7a15466cf44d8b8c831b7579d4977-1024x595.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735729-20f7a15466cf44d8b8c831b7579d4977-768x446.png 768w\" sizes=\"auto, (max-width: 1270px) 100vw, 1270px\" \/><\/figure>\n<\/div>\n\n\n<p>\u53ef\u4ee5\u65f6\u95f4\u7a7f\u68ad\uff0c\u5982\u679c\u63d0\u524d\u7a7f\u68ad\u6e05\u9664\u67d0\u4e8b\u4ef6\uff0c\u8be5\u4e8b\u4ef6\u4e4b\u540e\u7684\u5168\u90e8\u6d88\u5931<\/p>\n\n\n\n<p>\u70b9\u51fb\u4e0b\u8f7d\u6309\u94ae\u4f1a\u628a\u5f53\u524d\u64cd\u4f5c\u5168\u90e8\u6c47\u603b\u5230base state\u4e2d<\/p>\n\n\n\n<p>&nbsp;dispatch\u7684\u4f5c\u7528<\/p>\n\n\n\n<p>2.\u7ec4\u4ef6\u4e2d\u8bfb\u53d6vuex\u4e2d\u7684\u6570\u636e\uff1astore.state.sum<br>3.\u7ec4\u4ef6\u4e2d\u4fee\u6539vuex\u4e2d\u7684\u6570\u636e\uff1a$store.dispatch('action\u4e2d\u7684\u65b9\u6cd5\u540d'\uff0c\u6570\u636e)$store.commit('mutations\u4e2d\u7684\u65b9\u6cd5\u540d'\uff0c\u6570\u636e 66<br>\u5907\u6ce8\uff1a\u82e5\u6ca1\u6709\u7f51\u7edc\u8bf7\u6c42\u6216\u5176\u4ed6\u4e1a\u52a1\u903b\u8f91\uff0c\u7ec4\u4ef6\u4e2d\u4e5f\u53ef\u4ee5\u8d8a\u8fc7actions\uff0c\u5373\u4e0d\u5199dispatch\uff0c\u76f4\u63a5\u7f16\u5199commit<\/p>\n\n\n\n<p>getters\u53ef\u4ee5\u8de8\u7ec4\u4ef6\u4f7f\u7528\uff0c\u6bd4\u5982\u66ff\u4ee3computed\u7684\u4f7f\u7528\uff0c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5bf9\u4ee3\u7801\u7684\u4f18\u5316&nbsp;<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"223\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735891-c3e2c54e7d824e8cbec8c95d736d7a0e.png\" alt=\"\" class=\"wp-image-377\" style=\"width:662px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735891-c3e2c54e7d824e8cbec8c95d736d7a0e.png 720w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735891-c3e2c54e7d824e8cbec8c95d736d7a0e-300x93.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p>\u5199\u592a\u591a$store.state\u4e86\u00a0\uff0c\u4fee\u6539\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"662\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735903-c282410d18534c9697a69b76812b5d89.png\" alt=\"\" class=\"wp-image-378\" style=\"width:434px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735903-c282410d18534c9697a69b76812b5d89.png 718w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735903-c282410d18534c9697a69b76812b5d89-300x277.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<p>&nbsp;\u91cd\u5199sum\u6210return this.$store.state.sum\uff0c\u4ee5\u6b64\u7c7b\u63a8<\/p>\n\n\n\n<p>&nbsp;\u4f46\u662f\u7136\u540e\u8fdb\u4e00\u6b65\u601d\u8003\uff0cvuex\u5b98\u65b9\u6709\u4e00\u4e2a\u8fd9\u79cd\u6279\u91cf\u751f\u6210\u7684\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {mapState} from 'vuex'<img loading=\"lazy\" decoding=\"async\" height=\"15\" width=\"15\" src=\"blob:https:\/\/eve2333.top\/84be5b6e-cfbd-488a-b79a-dbdd22ae3b1a\"><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"812\" height=\"744\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735970-fbe26ed50abb4c329874877d139ed0b0.png\" alt=\"\" class=\"wp-image-379\" style=\"width:508px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735970-fbe26ed50abb4c329874877d139ed0b0.png 812w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735970-fbe26ed50abb4c329874877d139ed0b0-300x275.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735970-fbe26ed50abb4c329874877d139ed0b0-768x704.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"291\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735982-aee58156464942ae94e9487724b681b2.png\" alt=\"\" class=\"wp-image-380\" style=\"width:508px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735982-aee58156464942ae94e9487724b681b2.png 644w, https:\/\/eve2333.top\/wp-content\/uploads\/2024\/10\/1729735982-aee58156464942ae94e9487724b681b2-300x136.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;4\u4e2amap\u7684\u65b9\u6cd5mapState mapGetters \u4e0emapActions mapMutaions<\/h3>\n\n\n\n<p>\u524d\u4e24\u4e2a\u4e3b\u8981\u7528\u4e8ecomputed\u505a\u7b80\u5199\uff0c\u90a3\u540e\u4e24\u4e2a\u662fmethods\u7684\u7b80\u5199<\/p>\n\n\n\n<p><a href=\"https:\/\/www.bilibili.com\/video\/BV1Zy4y1K7SH\/?p=113&amp;spm_id_from=pageDriver&amp;vd_source=b31958e33027799323b8b7d77ae181d4\" target=\"_blank\"  rel=\"nofollow\" >113_\u5c1a\u7845\u8c37Vue\u6280\u672f_mapActions\u4e0emapMutations_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a>\u6700\u4f53\u73b0\u5bf9vue\u7406\u89e3\u7684\u4e00\u96c6<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u591a\u7ec4\u4ef6\u5171\u4eab\u6570\u636e<\/h1>\n\n\n\n<p>placeholder \u5c5e\u6027\u63d0\u4f9b\u53ef\u63cf\u8ff0\u8f93\u5165\u5b57\u6bb5\u9884\u671f\u503c\u7684\u63d0\u793a\u4fe1\u606f\uff08hint\uff09\u3002 &nbsp;\u8be5\u63d0\u793a\u4f1a\u5728\u8f93\u5165\u5b57\u6bb5\u4e3a\u7a7a\u65f6\u663e\u793a\uff0c\u5e76\u4f1a\u5728\u5b57\u6bb5\u83b7\u5f97\u7126\u70b9\u65f6\u6d88\u5931\u3002<\/p>\n\n\n\n<p>\u53ea\u8981\u5728mapState\u4e2d\u58f0\u660e\u4e00\u4e0bpersonList\u5373\u53ef<\/p>\n\n\n\n<p>Person.vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n   &lt;div&gt;\n     &lt;h1&gt;\u4eba\u5458\u5217\u8868&lt;\/h1&gt;\n     &lt;h2 style=\"color:skyblue;\"&gt;Count\u7ec4\u4ef6\u6c42\u548c\u4e3a:{{ sum }}&lt;\/h2&gt;\n     &lt;input type=\"text\" placeholder=\"\u8bf7\u8f93\u5165\u540d\u5b57\" v-model=\"name\" \/&gt;\n     &lt;button @click=\"add\"&gt;\u6dfb\u52a0&lt;\/button&gt;\n     &lt;ul&gt;\n       &lt;li v-for=\"p in personList\" :key=\"p.id\"&gt;{{ p.name }}&lt;\/li&gt;\n     &lt;\/ul&gt;\n   &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n\/\/ import { mapState } from 'vuex';\nimport {nanoid} from \"nanoid\";\nexport default {\n  name: \"Person\",\n  data(){\n    return {\n      name: '',\n    }\n  },\n  methods:{\n     add(){\n       const perObj = {\n         id: nanoid(),\n         name: this.name,\n       }\n       console.log(perObj);\n       this.name = '';\n       this.$store.commit('ADD_PERSON', perObj);\n     }\n  },\n  computed:{\n    \/\/ ...mapState(&#91;'personList']),\n    personList(){\n      return this.$store.state.personList;\n    },\n    sum(){\n      return this.$store.state.sum;\n    }\n  },\n}\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n   button{\n     margin-left: 5px;\n   }\n   ul{\n     margin-top: 5px;\n   }\n&lt;\/style&gt;\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u5c31\u6709\u4e24\u4e2a\u7ec4\u4ef6\u4e86\uff0c\u5728index.js\u4e2dstate\u6dfb\u52a0\uff0cCount.vue\u4fee\u6539\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n  &lt;div&gt;\n    &lt;h1&gt;\u5f53\u524d\u6c42\u548c\u4e3a: {{ sum }}&lt;\/h1&gt;\n    &lt;!--\u8ba9\u5176\u6536\u96c6\u5230\u7684\u6570\u636e\u5168\u662fnumber\u7c7b\u578b\u7684 number\u4fee\u9970\u7b26--&gt;\n    &lt;h3&gt;\u5f53\u524d\u6c42\u548c\u653e\u59273\u500d\u4e3a:{{ bigSum }}&lt;\/h3&gt;\n    &lt;h3&gt;\u6211\u5728{{ school }}, \u5b66\u4e60{{ subject }}&lt;\/h3&gt;\n    &lt;h3 style=\"color: red\"&gt;\u4e0b\u65b9\u5217\u8868\u7684\u603b\u4eba\u6570 {{ personList.length }}&lt;\/h3&gt;\n    &lt;select v-model.number=\"n\"&gt;\n      &lt;!--\u8ba9\u6240\u6709\u7684value\u5168\u90e8\u7ed1\u5b9a\u4e3a\u6570\u5b57--&gt;\n      &lt;option value=\"1\"&gt;1&lt;\/option&gt;\n      &lt;option value=\"2\"&gt;2&lt;\/option&gt;\n      &lt;option value=\"3\"&gt;3&lt;\/option&gt;\n    &lt;\/select&gt;\n    &lt;button @click=\"increment(n)\"&gt;+&lt;\/button&gt;\n    &lt;button @click=\"decrement(n)\"&gt;-&lt;\/button&gt;\n    &lt;button @click=\"incrementIfOdd(n)\"&gt;\u5f53\u524d\u6c42\u548c\u4e3a\u5947\u6570\u518d\u52a0&lt;\/button&gt;\n    &lt;button @click=\"incrementWait(n)\"&gt;\u7b49\u4e00\u7b49\u518d\u52a0&lt;\/button&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport { mapState, mapGetters, mapMutations, mapActions} from 'vuex';\nexport default {\n  \/\/\u8ba1\u6570\u7ec4\u4ef6\n  name: \"Count\",\n  data(){\n    return {\n      n: 1,\/\/ \u4ee3\u8868\u7528\u6237\u5728select\u6846\u5f00\u59cb\u7684\u65f6\u5019\u9009\u62e9\u7684\u6570\u5b57\n    }\n  },\n  computed:{\n    \/\/\u501f\u52a9mapState\u4ecestate\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ... mapState({\n    \/\/   sum:'sum',\n    \/\/   school: 'school',\n    \/\/   subject: 'subject'\n    \/\/ }),\n    \/\/\u501f\u52a9mapState\u4ecestate\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u6570\u7ec4\u5199\u6cd5(\u5373\u4ee3\u8868\u4e86\u751f\u6210\u7684\u8ba1\u7b97\u5c5e\u6027\u540d\u4e3asum\uff0c\u540c\u65f6\u4e5f\u4ee3\u8868\u4e86\u4ecestate\u627e\u5230sum)\n    ... mapState(&#91;'sum', 'school', 'subject', 'personList']),\n\n    \/\/\u501f\u52a9mapGetters\u4ecegetters\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ...mapGetters({ bigSum: 'bigSum' }),\n    \/\/\u501f\u52a9mapGetters\u4ecegetters\u4e2d\u751f\u6210\u8ba1\u7b97\u5c5e\u6027,\u6570\u7ec4\u5199\u6cd5\n    ...mapGetters(&#91;'bigSum']),\n\n  },\n  methods:{\n    \/\/ increment(){\n    \/\/   this.$store.commit('INCREMENT', this.n);\n    \/\/ },\n    \/\/ decrement(){\n    \/\/   this.$store.commit('DECREMENT', this.n);\n    \/\/ },\n    \/\/\u501f\u52a9mapMutations\u751f\u6210\u5bf9\u5e94\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4f1a\u8c03\u7528commit\u53bb\u8054\u7cfbmutations\uff0c\u5bf9\u8c61\u5199\u6cd5\n    ...mapMutations({\n      increment: 'INCREMENT',\n      decrement: 'DECREMENT',\n    }),\n    \/\/\u501f\u52a9\u6570\u7ec4\u5199\u6cd5\u751f\u6210\u65b9\u6cd5,\u4f46\u6ce8\u610f\u4f60\u751f\u6210\u7684\u65b9\u6cd5\u540d\u548cmutations\u91cc\u5bf9\u5e94\u7684\u65b9\u6cd5\u540d\u5c06\u4f1a\u4e00\u6837\u7684\n    \/\/ ...mapMutations(&#91;'increment', 'decrement']),\n\n    \/\/ incrementOdd(){\n    \/\/   this.$store.dispatch('incrementIfOdd', this.n);\n    \/\/ },\n    \/\/ incrementWait(){\n    \/\/   this.$store.dispatch('incrementWait', this.n);\n    \/\/ }\n\n    \/\/\u501f\u52a9mapMutations\u751f\u6210\u5bf9\u5e94\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4f1a\u8c03\u7528dispatch\u53bb\u8054\u7cfbactions\uff0c\u5bf9\u8c61\u5199\u6cd5\n    \/\/ ...mapActions({\n    \/\/   incrementIfOdd: 'incrementIfOdd',\n    \/\/   incrementWait: 'incrementWait',\n    \/\/ }),\n\n    ...mapActions(&#91;'incrementWait', 'incrementIfOdd']), \/\/\u6570\u7ec4\u5199\u6cd5,\u540c\u4e0a\n  },\n}\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n   button{\n     margin-left: 5px;\n   }\n&lt;\/style&gt;\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5199\u6210...mapMutations({'add':'ADD_PERSON'})\uff0c\u90a3\u4e48\u5728\u6a21\u677f\u91ccbutton\u7684click\u4e8b\u4ef6\u8981\u4f20\u53c2 @click=\"add({id:id,name:name})\"<\/p>\n\n\n\n<p>index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u8be5\u6587\u4ef6\u7528\u4e8e\u521b\u5efavuex\u4e2d\u6700\u6838\u5fc3\u7684store\n *\/\n\n\/\/\u5f15\u5165Vuex\nimport Vuex from 'vuex';\nimport Vue from \"vue\";\n\n\/\/\u4f7f\u7528vuex\u6765\u96c6\u4e2d\u7ba1\u7406\u72b6\u6001,\u5fc5\u8981\n\/\/new store\u7684\u524d\u63d0\u662f\u5fc5\u987b\u8981\u4f7f\u7528Vuex\u63d2\u4ef6\nVue.use(Vuex);\n\n\/\/\u521b\u5efaactions(\u672c\u8d28\u5c31\u662f\u5bf9\u8c61) \u7528\u4e8e\u54cd\u5e94\u7ec4\u4ef6\u4e2d\u7684\u52a8\u4f5c\nconst actions = {\n    \/\/\u6536\u5230\u4e0a\u4e0b\u6587\u5bf9\u8c61(\u5305\u542bcommit)\u548cdispatch\u8fc7\u6765\u7684\u503c\n    \/\/ increment(context, value){\n    \/\/     context.commit('INCREMENT', value);\n    \/\/ },\n    \/\/ decrement(context, value){\n    \/\/     context.commit('DECREMENT', value);\n    \/\/ },\n    incrementIfOdd(context, value){\n        \/\/ console.log(`action\u4e2d\u7684incrementIfOdd\u88ab\u8c03\u7528`);\n        \/\/ console.log('\u5904\u7406\u4e86\u4e00\u4e9b\u4e8b\u60c5');\n        \/\/ context.dispatch('demo1', value);\n        if(context.state.sum % 2) {\n            console.log('@')\n            context.commit('INCREMENT',value);\n            \/\/ context.state.sum += 1;\/\/\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u4f46\u662f\u8bb0\u4f4f\u672c\u6b21\u5bf9\u72b6\u6001\u7684\u6539\u53d8\u5f00\u53d1\u8005\u5de5\u5177\u5c06\u65e0\u6cd5\u6355\u83b7\uff0c\u56e0\u4e3a\u5f00\u53d1\u8005\u5de5\u5177\u662f\u5bf9mutations\u5bf9\u8bdd\u7684\n        }\n    },\n    incrementWait(context, value){\n        setTimeout(() => {\n            context.commit('INCREMENT', value);\n        },500)\n    },\n    \/\/ demo1(context, value){\n    \/\/     console.log('\u5904\u7406\u4e86\u4e00\u4e9b\u4e8b\u60c5---demo1');\n    \/\/     context.dispatch('demo2', value);\n    \/\/ },\n    \/\/ demo2(context, value){\n    \/\/     console.log('\u5728action\u7684demo\u4e2d\u5b8c\u6210\u6700\u7ec8\u7684\u903b\u8f91');\n    \/\/     if(context.state.sum % 2) {\n    \/\/         console.log('@')\n    \/\/         context.commit('INCREMENT',value);\n    \/\/     }\n    \/\/ }\n}\n\n\/\/\u521b\u5efamutations(\u672c\u8d28\u4e5f\u662f\u5bf9\u8c61) \u7528\u4e8e\u4fee\u6539\u6570\u636e(state)\nconst mutations = {\n    \/\/\u6536\u5230state\u548c\u8981\u64cd\u4f5c\u6570value\n    INCREMENT(state, value) {\n        state.sum += value;\n    },\n    DECREMENT(state, value) {\n        state.sum -= value;\n    },\n    ADD_PERSON(state, value){\n        state.personList.unshift(value);\n    }\n}\n\n\/\/\u51c6\u5907getters\u7528\u4e8e\u52a0\u5de5state\uff0c\u5c06\u5176\u5171\u4eab\u4e8e\u5404\u4e2a\u7ec4\u4ef6\u5f53\u4e2d\nconst getters = {\n    bigSum(state){\n        return state.sum * 10;\n    }\n}\n\n\/\/\u51c6\u5907state(\u6570\u636e) \u5b58\u50a8\u6570\u636e\n\/\/\u7c7b\u4f3c\u4e8e\u5404\u4e2a\u7ec4\u4ef6\u91cc\u7684computed(\u8ba1\u7b97\u5c5e\u6027),\u53ea\u4e0d\u8fc7\u5b83\u662f\u5171\u4eab\u7684\nconst state = {\n    sum: 0,\n    school: 'Wust',\n    subject: 'Computer Science',\n    personList: &#91;\n        { id: '001', name: '\u5f20\u4e09'}\n    ],\n}\n\n\n\/\/\u521b\u5efa\u5e76\u66b4\u9732store\nexport default new Vuex.Store({\n    actions,\n    mutations,\n    state,\n    getters\n});<img loading=\"lazy\" decoding=\"async\" height=\"15\" width=\"15\" src=\"blob:https:\/\/eve2333.top\/19f55aae-1133-405b-9e63-090cba6ba602\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>vue-resource \u5728vue1.0\u65f6\u4ee3\u8bb2\u7684\u6bd4\u8f83\u591a\uff0c\u662fvue.\u63d2\u4ef6\u5e93\uff0c \u5728vc\u548cvm\u4e2d\u4f1a\u591a\u51fa\u5982\u4e0bF12\u4ee3\u7801\u5373\uff0c$http:() &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[12,11,10],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-2","tag-js","tag-vue","tag-10"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":0,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}