{"id":5981,"date":"2025-06-14T10:09:53","date_gmt":"2025-06-17T02:21:52","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5981"},"modified":"2025-06-17T10:09:53","modified_gmt":"2025-06-17T02:21:52","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8vue-js%e4%b8%ad%e7%9a%84%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/vue-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8vue-js%e4%b8%ad%e7%9a%84%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Vue.js \u7d44\u4ef6\u901a\u4fe1\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue, \u7ec4\u4ef6\u901a\u4fe1, Vue.js, props, events, $emit, $on, $parent, $children\"><\/p>\n<h1>\u5728 Vue \u4e2d\u5982\u4f55\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\uff1f<\/h1>\n<p>Vue.js \u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684 JavaScript \u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5feb\u901f\u69cb\u5efa\u9ad8\u54c1\u8cea\u7684 Web \u61c9\u7528\u7a0b\u5f0f\u3002\u7d44\u4ef6\u901a\u4fe1\u5728 Vue \u4e2d\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u72c0\u614b\u8207\u6578\u64da\u6d41\u3002\u672c\u6587\u5c07\u4ecb\u7d39 2025 \u5e74\u6700\u65b0\u7684\u7d44\u4ef6\u901a\u4fe1\u65b9\u5f0f\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u8207\u932f\u8aa4\u6392\u9664\u5efa\u8b70\uff0c\u8b93\u4f60\u80fd\u5920\u66f4\u8f15\u9b06\u5730\u958b\u767c\u591a\u5c64\u6b21\u7684\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<h2>Vue \u7684\u7d44\u4ef6\u901a\u4fe1\u65b9\u5f0f<\/h2>\n<p>Vue \u63d0\u4f9b\u4e86\u591a\u7a2e\u7d44\u4ef6\u9593\u901a\u4fe1\u7684\u65b9\u5f0f\uff0c\u5305\u62ec <strong style=\"color: #0099cc;\">props<\/strong>\u3001<strong style=\"color: #0099cc;\">events<\/strong>\u3001<strong style=\"color: #0099cc;\">$emit<\/strong>\u3001<strong style=\"color: #0099cc;\">$on<\/strong>\u3001<strong style=\"color: #0099cc;\">$parent<\/strong> \u548c <strong style=\"color: #0099cc;\">$children<\/strong> \u7b49\u3002\u4ee5\u4e0b\u5c07\u9010\u4e00\u4ecb\u7d39\u9019\u4e9b\u65b9\u5f0f\u53ca\u5176\u61c9\u7528\u3002<\/p>\n<h3>1. \u4f7f\u7528 Props<\/h3>\n<p>Props \u662f Vue \u4e2d\u6700\u5e38\u7528\u7684\u7d44\u4ef6\u9593\u901a\u4fe1\u65b9\u5f0f\u3002\u5b83\u5141\u8a31\u7236\u7d44\u4ef6\u5c07\u6578\u64da\u50b3\u905e\u7d66\u5b50\u7d44\u4ef6\uff0c\u5b50\u7d44\u4ef6\u5247\u53ef\u4ee5\u901a\u904e props \u63a5\u6536\u9019\u4e9b\u6578\u64da\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u7236\u7d44\u4ef6\nVue.component('parent-component', {\n  template: '<div><child-component :message=\"message\"><\/child-component><\/div>',\n  data: function () {\n    return {\n      message: 'Hello World!'\n    }\n  }\n})\n\n\/\/ \u5b50\u7d44\u4ef6\nVue.component('child-component', {\n  props: ['message'],\n  template: '<div>{{ message }}<\/div>'\n})\n<\/code><\/pre>\n<h3>2. \u4e8b\u4ef6 (Events)<\/h3>\n<p>\u5b50\u7d44\u4ef6\u53ef\u4ee5\u901a\u904e\u89f8\u767c\u4e8b\u4ef6\u5c07\u6578\u64da\u767c\u9001\u56de\u7236\u7d44\u4ef6\u3002\u7236\u7d44\u4ef6\u4f7f\u7528 <strong>$on<\/strong> \u65b9\u6cd5\u4f86\u76e3\u807d\u9019\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u7236\u7d44\u4ef6\nVue.component('parent-component', {\n  template: '<div><child-component @send=\"handleSend\"><\/child-component><\/div>',\n  methods: {\n    handleSend: function (data) {\n      console.log(data)  \/\/ \u5728\u63a7\u5236\u53f0\u986f\u793a\u50b3\u905e\u7684\u6578\u64da\n    }\n  }\n})\n\n\/\/ \u5b50\u7d44\u4ef6\nVue.component('child-component', {\n  template: '<div><button @click=\"handleClick\">Send<\/button><\/div>',\n  methods: {\n    handleClick: function () {\n      this.$emit('send', 'Hello World!')\n    }\n  }\n})\n<\/code><\/pre>\n<h3>3. \u4f7f\u7528 <span class=\"katex math inline\">parent \u548c<\/span>children<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u5b50\u7d44\u4ef6\u53ef\u80fd\u9700\u8981\u76f4\u63a5\u8207\u7236\u7d44\u4ef6\u9032\u884c\u4ea4\u4e92\uff0c\u9019\u6642\u53ef\u4ee5\u4f7f\u7528 <strong>$parent<\/strong> \u548c <strong>$children<\/strong>\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u7236\u7d44\u4ef6\nVue.component('parent-component', {\n  template: '<div><child-component ref=\"child\"><\/child-component><button @click=\"callChild\">Call Child<\/button><\/div>',\n  methods: {\n    callChild() {\n      this.$refs.child.handleFromParent();\n    }\n  }\n})\n\n\/\/ \u5b50\u7d44\u4ef6\nVue.component('child-component', {\n  template: '<div>Child Component<\/div>',\n  methods: {\n    handleFromParent() {\n      console.log('Called from Parent');\n    }\n  }\n})\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5982\uff1a<\/p>\n<p>&#8211; **\u672a\u6b63\u78ba\u50b3\u905e props\uff1a** \u78ba\u4fdd\u7236\u7d44\u4ef6\u4e2d\u4f7f\u7528\u7684 props \u540d\u7a31\u8207\u5b50\u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u7684\u4e00\u81f4\u3002<br \/>\n&#8211; **\u4e8b\u4ef6\u672a\u88ab\u76e3\u807d\uff1a** \u78ba\u4fdd\u7236\u7d44\u4ef6\u4f7f\u7528 <span class=\"katex math inline\">on \u6b63\u78ba\u76e3\u807d\u5b50\u7d44\u4ef6\u7684\u4e8b\u4ef6\u3002<br \/>\n&#8211; **\u4f7f\u7528<\/span>parent \u548c $children \u6642\u8981\u5c0f\u5fc3\uff1a** \u9019\u4e9b\u5c6c\u6027\u53ef\u80fd\u6703\u5c0e\u81f4\u4e0d\u5fc5\u8981\u7684\u8026\u5408\uff0c\u5efa\u8b70\u5118\u91cf\u4f7f\u7528 props \u548c events\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u7d44\u4ef6\u901a\u4fe1\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u7236\u5b50\u7d44\u4ef6\u9593\u7684\u6578\u64da\u50b3\u905e\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u66f4\u8907\u96dc\u7684\u5834\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u4f7f\u7528 Vuex \u7ba1\u7406\u5168\u5c40\u72c0\u614b\uff1a** \u7576\u61c9\u7528\u7a0b\u5f0f\u8b8a\u5f97\u8907\u96dc\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 Vuex \u4f86\u96c6\u4e2d\u7ba1\u7406\u72c0\u614b\uff0c\u5f9e\u800c\u907f\u514d\u591a\u5c64\u7d44\u4ef6\u7684 props \u50b3\u905e\u3002<br \/>\n&#8211; **\u4f7f\u7528\u63d2\u69fd (Slots)\uff1a** \u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528\u63d2\u69fd\u53ef\u4ee5\u8b93\u7236\u7d44\u4ef6\u63d2\u5165\u81ea\u5b9a\u7fa9\u5167\u5bb9\uff0c\u63d0\u9ad8\u7d44\u4ef6\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Vue.js \u63d0\u4f9b\u4e86\u591a\u7a2e\u7d44\u4ef6\u9593\u901a\u4fe1\u7684\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\uff0c\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5f0f\u8a2d\u8a08\u3002\u638c\u63e1\u9019\u4e9b\u901a\u4fe1\u65b9\u5f0f\u5c07\u6709\u52a9\u65bc\u4f60\u5728 Vue \u7684\u958b\u767c\u65c5\u7a0b\u4e2d\u66f4\u70ba\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Vue.js \u4e2d\u7684 props \u548c events \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>Props \u7528\u65bc\u7236\u7d44\u4ef6\u5411\u5b50\u7d44\u4ef6\u50b3\u905e\u6578\u64da\uff0c\u800c events \u5247\u662f\u5b50\u7d44\u4ef6\u5411\u7236\u7d44\u4ef6\u767c\u9001\u6d88\u606f\u7684\u65b9\u5f0f\u3002<\/p>\n<h3>2. \u7576\u6211\u61c9\u8a72\u4f7f\u7528 $emit \u800c\u4e0d\u662f props\uff1f<\/h3>\n<p>\u7576\u9700\u8981\u8b93\u5b50\u7d44\u4ef6\u901a\u77e5\u7236\u7d44\u4ef6\u67d0\u500b\u4e8b\u4ef6\u767c\u751f\u6642\uff0c\u61c9\u4f7f\u7528 $emit\uff1b\u800c\u7576\u9700\u8981\u50b3\u905e\u6578\u64da\u6642\uff0c\u5247\u4f7f\u7528 props\u3002<\/p>\n<h3>3. Vuex \u662f\u4ec0\u9ebc\uff0c\u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u4f7f\u7528\u5b83\uff1f<\/h3>\n<p>Vuex \u662f\u4e00\u500b\u72c0\u614b\u7ba1\u7406\u5eab\uff0c\u7576\u61c9\u7528\u7a0b\u5f0f\u8b8a\u5f97\u8907\u96dc\uff0c\u7d44\u4ef6\u4e4b\u9593\u7684\u6578\u64da\u5171\u4eab\u9700\u6c42\u589e\u591a\u6642\uff0c\u61c9\u8003\u616e\u4f7f\u7528 Vuex \u4f86\u96c6\u4e2d\u7ba1\u7406\u72c0\u614b\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aVue.js\u662f\u4e00\u500b\u5f37\u5927\u7684JavaScript\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5275\u5efa\u548c\u7ba1\u7406\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Vue.js\u4e2d\u7684\u7d44\u4ef6\u901a\u4fe1\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u66f4\u6709\u6548\u5730\u7ba1\u7406\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[180,179],"tags":[178,177],"class_list":["post-5981","post","type-post","status-publish","format-standard","hentry","category-vue","category-vue-js","tag-vue","tag-vue-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1yt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5981"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5981\/revisions"}],"predecessor-version":[{"id":5982,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5981\/revisions\/5982"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}