{"id":96338,"date":"2024-04-11T15:01:30","date_gmt":"2024-04-11T07:01:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/96338.html"},"modified":"2024-04-11T15:01:31","modified_gmt":"2024-04-11T07:01:31","slug":"vue-js-%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%85%a8%e5%b1%80%e5%87%bd%e6%95%b0%e6%88%96%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/96338.html","title":{"rendered":"Vue.js \u5982\u4f55\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172613\/1a58a8bc-eca5-4c8b-8d88-777f1cc04ea4.webp\" alt=\"Vue.js \u5982\u4f55\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\" \/><\/p>\n<p><p>\u5728Vue.js\u4e2d\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u53ef\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e<strong>\u5229\u7528Vue\u7684\u539f\u578b\u94fe\u3001\u4f7f\u7528\u63d2\u4ef6\u3001\u5229\u7528Vuex\u72b6\u6001\u7ba1\u7406<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u7ec4\u4ef6\u4e2d\u590d\u7528\u51fd\u6570\u6216\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u3002<strong>\u5229\u7528Vue\u7684\u539f\u578b\u94fe<\/strong>\u662f\u5176\u4e2d\u4e00\u4e2a\u65e2\u7b80\u5355\u53c8\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u5728Vue\u7684\u539f\u578b\u4e0a\u6dfb\u52a0\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u8fd9\u6837\u6240\u6709\u7684Vue\u5b9e\u4f8b\uff08\u5305\u62ec\u6240\u6709\u7684Vue\u7ec4\u4ef6\uff09\u90fd\u80fd\u591f\u8bbf\u95ee\u8fd9\u4e9b\u5c5e\u6027\u6216\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5229\u7528Vue\u7684\u539f\u578b\u94fe<\/h3>\n<\/p>\n<p><p>Vue\u7684\u539f\u578b\u94fe\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002\u901a\u8fc7\u5728Vue\u7684\u539f\u578b\u4e0a\u6dfb\u52a0\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u7684Vue\u5b9e\u4f8b\u90fd\u80fd\u591f\u8bbf\u95ee\u5230\u8fd9\u4e9b\u5168\u5c40\u7684\u8d44\u6e90\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c24\u5176\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u5728\u591a\u4e2a\u7ec4\u4ef6\u4e2d\u590d\u7528\u7684\u51fd\u6570\u6216\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4e3a\u4e86\u5728Vue\u7684\u539f\u578b\u4e0a\u6dfb\u52a0\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u5728\u9879\u76ee\u521d\u59cb\u5316\u9636\u6bb5\uff0c\u4e5f\u5c31\u662f\u5728\u521b\u5efaVue\u6839\u5b9e\u4f8b\u4e4b\u524d\u505a\u8fd9\u4e9b\u4e8b\u60c5\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5168\u5c40\u7684\u5e2e\u52a9\u51fd\u6570\uff0c\u4f60\u60f3\u5728\u591a\u4e2a\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u5b83\uff0c\u53ea\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u81ea\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u51fd\u6570<\/p>\n<p>Vue.prototype.$myGlobalFunction = function () {<\/p>\n<p>  \/\/ \u51fd\u6570\u5185\u5bb9<\/p>\n<p>  alert(&#039;This is a global function&#039;);<\/p>\n<p>};<\/p>\n<p>\/\/ \u73b0\u5728\uff0c$myGlobalFunction\u6210\u4e3a\u4e86\u6240\u6709Vue\u5b9e\u4f8b\u7684\u53ef\u8bbf\u95ee\u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u8fd9\u6837\u505a\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u7ec4\u4ef6\u4e2d\u901a\u8fc7<code>this.$myGlobalFunction()<\/code>\u6765\u8c03\u7528\u8fd9\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u590d\u5b9a\u4e49\u6216\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u63d2\u4ef6<\/h3>\n<\/p>\n<p><p>Vue\u63d2\u4ef6\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u5411Vue\u5e94\u7528\u6dfb\u52a0\u5168\u5c40\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u9002\u5408\u4e8e\u6dfb\u52a0\u4e00\u7ec4\u529f\u80fd\uff0c\u6216\u8005\u5f53\u4f60\u60f3\u8981\u628a\u8fd9\u4e9b\u5168\u5c40\u529f\u80fd\u5c01\u88c5\u8d77\u6765\u4ee5\u4fbf\u4e8e\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e4b\u95f4\u5171\u4eab\u3002\u521b\u5efaVue\u63d2\u4ef6\u76f8\u5bf9\u7b80\u5355\uff0c\u4f60\u9700\u8981\u505a\u7684\u5c31\u662f\u63d0\u4f9b\u4e00\u4e2a<code>install<\/code>\u65b9\u6cd5\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u5411Vue\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const MyPlugin = {<\/p>\n<p>  install(Vue, options) {<\/p>\n<p>    Vue.prototype.$myGlobalFunction = function () {<\/p>\n<p>      \/\/ \u51fd\u6570\u5185\u5bb9<\/p>\n<p>    };<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>\/\/ \u4f7f\u7528\u63d2\u4ef6<\/p>\n<p>Vue.use(MyPlugin);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u5f53\u4f60\u7684\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u9700\u8981\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u590d\u7528\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u5229\u7528Vuex\u72b6\u6001\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u7ec4\u4ef6\u95f4\u5171\u4eab\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528Vuex\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u662f\u4e00\u79cd\u66f4\u52a0\u7ed3\u6784\u5316\u548c\u7ef4\u62a4\u6027\u66f4\u597d\u7684\u65b9\u6cd5\u3002Vuex\u662fVue\u5b98\u65b9\u63d0\u4f9b\u7684\u72b6\u6001\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u96c6\u4e2d\u5f0f\u5b58\u50a8\u7ba1\u7406\u5e94\u7528\u7684\u6240\u6709\u7ec4\u4ef6\u7684\u72b6\u6001\uff0c\u5e76\u4ee5\u76f8\u5e94\u7684\u89c4\u5219\u4fdd\u8bc1\u72b6\u6001\u4ee5\u4e00\u79cd\u53ef\u9884\u6d4b\u7684\u65b9\u5f0f\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const store = new Vuex.Store({<\/p>\n<p>  state: {<\/p>\n<p>    count: 0<\/p>\n<p>  },<\/p>\n<p>  mutations: {<\/p>\n<p>    increment(state) {<\/p>\n<p>      state.count++<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Vuex\u4e2d\uff0c\u72b6\u6001\u5b58\u50a8\u662f\u54cd\u5e94\u5f0f\u7684\u3002\u5f53Vue\u7ec4\u4ef6\u4ecestore\u4e2d\u8bfb\u53d6\u72b6\u6001\u7684\u65f6\u5019\uff0c\u82e5\u72b6\u6001\u53d1\u751f\u53d8\u5316\uff0c\u90a3\u4e48\u76f8\u5e94\u7684\u7ec4\u4ef6\u4e5f\u4f1a\u76f8\u5e94\u5730\u5f97\u5230\u9ad8\u6548\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<strong>\u5982\u679c\u4f60\u4ec5\u4ec5\u9700\u8981\u5728\u5168\u5c40\u8303\u56f4\u5185\u590d\u7528\u4e00\u4e9b\u7b80\u5355\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u5229\u7528Vue\u7684\u539f\u578b\u94fe\u662f\u4e00\u4e2a\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u5f53\u4f60\u5e0c\u671b\u6dfb\u52a0\u7684\u5168\u7403\u529f\u80fd\u66f4\u4e3a\u590d\u6742\u6216\u5e0c\u671b\u80fd\u591f\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u590d\u7528\u65f6\uff0c\u521b\u5efaVue\u63d2\u4ef6\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u800c\u5bf9\u4e8e\u5168\u5c40\u72b6\u6001\u7684\u7ba1\u7406\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528Vuex\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u72b6\u6001\uff0c\u867d\u7136\u524d\u671f\u9700\u8981\u66f4\u591a\u7684\u8bbe\u7f6e\uff0c\u4f46\u5bf9\u9879\u76ee\u7684\u957f\u671f\u7ef4\u62a4\u5927\u6709\u76ca\u5904\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Vue.js\u4e2d\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\uff1f<\/strong><\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7Vue.prototype\u6765\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u3002\u53ea\u9700\u8981\u5728Vue\u5b9e\u4f8b\u5316\u4e4b\u524d\u5728Vue.prototype\u4e0a\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u4f1a\u6210\u4e3aVue.js\u4e2d\u6240\u6709\u7ec4\u4ef6\u7684\u5168\u5c40\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u6dfb\u52a0\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">Vue.prototype.$myFunction = function() {\n  \/\/ \u8fd9\u91cc\u662f\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\n}\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55Vue\u7ec4\u4ef6\u4e2d\u901a\u8fc7<code>this.$myFunction()<\/code>\u6765\u8c03\u7528\u8be5\u5168\u5c40\u51fd\u6570\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u6dfb\u52a0\u5168\u5c40\u53d8\u91cf\uff1a<\/strong> \u4f7f\u7528Vue\u63d2\u4ef6\u6765\u6dfb\u52a0\u5168\u5c40\u53d8\u91cf\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u63d2\u4ef6\uff0c\u5c06\u5168\u5c40\u53d8\u91cf\u6dfb\u52a0\u5230Vue\u5b9e\u4f8b\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u63d2\u4ef6\u7684\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">\/\/ myPlugin.js\nconst myPlugin = {\n  install(Vue) {\n    Vue.prototype.$myVariable = &#039;Global Variable&#039;\n  }\n}\n\nexport default myPlugin\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728\u4f60\u7684\u5165\u53e3\u6587\u4ef6\uff08\u5982m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.js\uff09\u4e2d\uff0c\u4f7f\u7528\u8be5\u63d2\u4ef6\u6765\u6dfb\u52a0\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-javascript\">import Vue from &#039;vue&#039;\nimport myPlugin from &#039;.\/myPlugin&#039;\n\nVue.use(myPlugin)\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u5728\u4efb\u4f55Vue\u7ec4\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>this.$myVariable<\/code>\u6765\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528Vuex\u6765\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\uff1a<\/strong> \u5982\u679c\u4f60\u7684\u9879\u76ee\u6d89\u53ca\u5230\u8f83\u5927\u578b\u7684\u72b6\u6001\u7ba1\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Vuex\u3002Vuex\u662f\u4e00\u4e2a\u4e13\u4e3aVue.js\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u72b6\u6001\u7ba1\u7406\u6a21\u5f0f\u3002<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728Vuex\u7684state\u4e2d\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff0c\u7136\u540e\u5728\u4efb\u4f55Vue\u7ec4\u4ef6\u4e2d\u901a\u8fc7<code>this.$store.state.myVariable<\/code>\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u8be5\u53d8\u91cf\u3002<\/p>\n<p>\u8fd9\u662fVue.js\u4e2d\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u7684\u51e0\u79cd\u65b9\u6cd5\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u65b9\u6cd5\u6765\u6269\u5c55Vue.js\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Vue.js\u4e2d\u6dfb\u52a0\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u53ef\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u5229\u7528Vue\u7684\u539f\u578b\u94fe\u3001\u4f7f\u7528\u63d2\u4ef6\u3001\u5229\u7528Vuex\u72b6 [&hellip;]","protected":false},"author":3,"featured_media":96339,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/96338"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=96338"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/96338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/96339"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=96338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=96338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=96338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}