{"id":4357,"date":"2025-06-10T09:35:10","date_gmt":"2025-06-17T01:41:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4357"},"modified":"2025-06-17T09:35:10","modified_gmt":"2025-06-17T01:41:09","slug":"%e5%ad%b8%e7%bf%92vue-js%e4%b8%ad%e7%9a%84%e7%b5%84%e4%bb%b6%e6%96%b9%e6%b3%95%ef%bc%88methods%ef%bc%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/vue-js\/%e5%ad%b8%e7%bf%92vue-js%e4%b8%ad%e7%9a%84%e7%b5%84%e4%bb%b6%e6%96%b9%e6%b3%95%ef%bc%88methods%ef%bc%89\/","title":{"rendered":"\u5168\u9762\u5b78\u7fd2 Vue.js \u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09| 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue.js, \u7d44\u4ef6\u65b9\u6cd5, methods, Vue 3, JavaScript, \u7d44\u4ef6\u8a2d\u8a08\"><\/p>\n<h2>\u4ecb\u7d39 Vue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09<\/h2>\n<p>Vue.js \u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684 JavaScript \u6846\u67b6\uff0c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u958b\u767c\u9ad8\u54c1\u8cea\u7684 Web \u61c9\u7528\u7a0b\u5f0f\u3002Vue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5c07\u7a0b\u5f0f\u78bc\u62c6\u5206\u6210\u4e0d\u540c\u7684\u7d44\u4ef6\uff0c\u4e26\u5728\u9019\u4e9b\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u8cc7\u6599\u548c\u65b9\u6cd5\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u78bc\u7684\u53ef\u91cd\u7528\u6027\u548c\u7dad\u8b77\u6027\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Vue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09<\/h2>\n<p>\u5728 Vue.js \u4e2d\uff0c\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09\u5141\u8a31\u4f60\u5728\u7d44\u4ef6\u5167\u5b9a\u7fa9\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u88ab\u8a72\u7d44\u4ef6\u7684\u6a21\u677f\u76f4\u63a5\u8abf\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9a\u7fa9\u548c\u4f7f\u7528\u9019\u4e9b\u65b9\u6cd5\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u5b9a\u7fa9\u4e00\u500b\u7d44\u4ef6\nVue.component('my-component', {\n  data() {\n    return {\n      message: 'Hello from my-component!'\n    };\n  },\n  methods: {\n    \/\/ \u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\n    myFunction() {\n      console.log(this.message); \/\/ \u8f38\u51fa: Hello from my-component!\n    }\n  }\n});\n\n\/\/ \u5728\u53e6\u4e00\u500b\u7d44\u4ef6\u4e2d\u4f7f\u7528\u5b83\nVue.component('another-component', {\n  components: {\n    'my-component': myComponent\n  },\n  methods: {\n    \/\/ \u4f7f\u7528 myFunction \u51fd\u6578\n    callMyFunction() {\n      this.$refs.myComponent.myFunction(); \/\/ \u901a\u904e\u5f15\u7528\u8abf\u7528\n    }\n  },\n  template: `<div><my-component ref=\"myComponent\"><\/my-component><button @click=\"callMyFunction\">\u547c\u53eb\u65b9\u6cd5<\/button><\/div>`\n});\n<\/pre>\n<h2>Vue.js \u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09\u7684\u512a\u9ede<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff1a<\/strong>\u62c6\u5206\u7a0b\u5f0f\u78bc\u70ba\u4e0d\u540c\u7684\u7d44\u4ef6\uff0c\u8b93\u6bcf\u500b\u7d44\u4ef6\u7684\u529f\u80fd\u66f4\u52a0\u660e\u78ba\uff0c\u5f9e\u800c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u91cd\u8907\u7684\u7a0b\u5f0f\u78bc\uff1a<\/strong>\u53ef\u5728\u591a\u500b\u7d44\u4ef6\u9593\u91cd\u7528\u65b9\u6cd5\u548c\u8cc7\u6599\uff0c\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u8907\u3002<\/li>\n<li><strong>\u66f4\u5bb9\u6613\u7dad\u8b77\uff1a<\/strong>\u7d50\u69cb\u6e05\u6670\u7684\u7d44\u4ef6\u8a2d\u8a08\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u7684\u7dad\u8b77\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/li>\n<\/ul>\n<h2>\u5e38\u898b\u932f\u8aa4\u53ca\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 Vue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u65b9\u6cd5\u672a\u6b63\u78ba\u8abf\u7528\uff1a<\/strong>\u78ba\u8a8d\u4f7f\u7528 <code>this<\/code> \u95dc\u9375\u5b57\u7684\u6b63\u78ba\u6027\uff0c\u4e26\u78ba\u4fdd\u65b9\u6cd5\u5728\u7d44\u4ef6\u4e0a\u4e0b\u6587\u4e2d\u88ab\u8abf\u7528\u3002<\/li>\n<li><strong>\u7121\u6cd5\u8a2a\u554f\u8cc7\u6599\uff1a<\/strong>\u5728\u65b9\u6cd5\u5167\u90e8\u8a2a\u554f\u7684\u8cc7\u6599\u9700\u4f7f\u7528 <code>this.dataProperty<\/code> \u7684\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u7d44\u4ef6\u65b9\u6cd5\u9084\u53ef\u4ee5\u8207 Vuex\uff08\u72c0\u614b\u7ba1\u7406\uff09\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u5728\u591a\u500b\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u72c0\u614b\u548c\u884c\u70ba\u3002\u9019\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cVue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5b83\u4e0d\u50c5\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u6709\u6548\u62c6\u5206\u7a0b\u5f0f\u78bc\uff0c\u9084\u80fd\u5728\u4e0d\u540c\u7d44\u4ef6\u9593\u5171\u4eab\u8cc7\u6599\u548c\u65b9\u6cd5\uff0c\u5927\u5e45\u63d0\u5347\u958b\u767c\u6548\u7387\u3001\u6e1b\u5c11\u91cd\u8907\u7a0b\u5f0f\u78bc\u4e26\u8b93\u7a0b\u5f0f\u78bc\u66f4\u5bb9\u6613\u7dad\u8b77\u3002<\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Vue.js \u7684\u7d44\u4ef6\u65b9\u6cd5\u8207\u8a08\u7b97\u5c6c\u6027\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>A1: \u7d44\u4ef6\u65b9\u6cd5\u7528\u65bc\u57f7\u884c\u7279\u5b9a\u64cd\u4f5c\uff0c\u800c\u8a08\u7b97\u5c6c\u6027\u5247\u7528\u65bc\u6839\u64da\u53cd\u61c9\u5f0f\u8cc7\u6599\u8a08\u7b97\u548c\u8fd4\u56de\u503c\u3002\u8a08\u7b97\u5c6c\u6027\u6703\u7de9\u5b58\u7d50\u679c\uff0c\u800c\u65b9\u6cd5\u5247\u5728\u6bcf\u6b21\u8abf\u7528\u6642\u57f7\u884c\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728\u7d44\u4ef6\u65b9\u6cd5\u4e2d\u4f7f\u7528\u5916\u90e8\u51fd\u6578\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u5c07\u5916\u90e8\u51fd\u6578\u5f15\u5165\u7d44\u4ef6\uff0c\u7136\u5f8c\u5728\u7d44\u4ef6\u65b9\u6cd5\u5167\u90e8\u8abf\u7528\u5b83\uff0c\u9019\u6a23\u53ef\u4ee5\u5be6\u73fe\u529f\u80fd\u7684\u6a21\u7d44\u5316\u3002<\/p>\n<h3>Q3: \u70ba\u4ec0\u9ebc\u6211\u7684\u65b9\u6cd5\u4e0d\u80fd\u88ab\u89f8\u767c\uff1f<\/h3>\n<p>A3: \u78ba\u4fdd\u65b9\u6cd5\u7684\u8abf\u7528\u662f\u5728\u6b63\u78ba\u7684\u4e0a\u4e0b\u6587\u4e2d\u9032\u884c\u7684\uff0c\u4e26\u4e14\u4f7f\u7528 <code>@click<\/code> \u6216\u5176\u4ed6\u4e8b\u4ef6\u6b63\u78ba\u5730\u7d81\u5b9a\u3002<\/p>\n<p>---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Vue.js\u4e2d\u7684\u7d44\u4ef6\u65b9\u6cd5\uff08methods\uff09\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u958b\u767c\u51fa\u66f4\u591a\u529f\u80fd\uff0c\u66f4\u5bb9\u6613\u7dad\u8b77\uff0c\u66f4\u5bb9\u6613\u64f4\u5c55\u7684\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-4357","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-18h","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4357","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=4357"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4357\/revisions"}],"predecessor-version":[{"id":4358,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4357\/revisions\/4358"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}