{"id":299393,"date":"2024-05-20T17:16:24","date_gmt":"2024-05-20T09:16:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299393.html"},"modified":"2024-05-20T17:16:34","modified_gmt":"2024-05-20T09:16:34","slug":"vue3-%e9%a1%b9%e7%9b%ae%e7%9a%84-setup-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299393.html","title":{"rendered":"Vue3 \u9879\u76ee\u7684 setup \u51fd\u6570\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27151547\/c7c01431-aa20-42fc-a5d1-74b1875421b4.webp\" alt=\"Vue3 \u9879\u76ee\u7684 setup \u51fd\u6570\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p>Vue3 \u9879\u76ee\u4e2d\u7684 setup \u51fd\u6570\u662f\u7ec4\u4ef6\u9009\u9879API\u7684\u66ff\u4ee3\uff0c\u7528\u6765\u5728\u7ec4\u4ef6\u521b\u5efa\u4e4b\u524d\u6267\u884c\u54cd\u5e94\u5f0f\u72b6\u6001\u7684\u58f0\u660e\u3001\u751f\u547d\u5468\u671f\u94a9\u5b50\u7684\u6ce8\u518c\u3001\u4ee5\u53ca\u63d0\u4f9b\u7ec4\u4ef6\u7684\u65b9\u6cd5\u548c\u6570\u636e\u3002\u5176\u7279\u70b9\u5305\u62ec\u54cd\u5e94\u5f0f\u6570\u636e\u7684\u521b\u5efa\u3001\u4f7f\u7528Composition API\u7ec4\u6210\u903b\u8f91\u590d\u7528\u3001\u8bbf\u95eeprops\u548ccontext\u5bf9\u8c61\u3001\u6ce8\u518c\u751f\u547d\u5468\u671f\u94a9\u5b50\u3002\u5176\u4e2d\uff0c\u54cd\u5e94\u5f0f\u6570\u636e\u7684\u521b\u5efa\u901a\u8fc7<code>reactive<\/code>\u6216<code>ref<\/code>\u51fd\u6570\u5b9e\u73b0\uff0c\u5c06\u7ec4\u4ef6\u7684\u72b6\u6001\u5305\u88c5\u6210\u53cd\u5e94\u5f0f\u5bf9\u8c61\uff0c\u4ee5\u4fbfVue\u80fd\u591f\u8ffd\u8e2a\u5176\u53d8\u5316\u5e76\u66f4\u65b0\u89c6\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SETUP \u51fd\u6570\u7684\u8c03\u7528\u65f6\u673a\u548c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>setup \u51fd\u6570\u5728\u7ec4\u4ef6\u5b9e\u4f8b\u521b\u5efa\u4e4b\u524d\u88ab\u8c03\u7528\uff0c\u6b64\u65f6\u7ec4\u4ef6\u7684props\u5df2\u88ab\u89e3\u6790\uff0c\u5e76\u4e14\u5728template\u548c\u5176\u4ed6\u7ec4\u4ef6\u9009\u9879\u4e4b\u524d\u88ab\u6267\u884c\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1aprops\u548ccontext\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>props<\/strong>: \u4e00\u4e2a\u5305\u542b\u7ec4\u4ef6\u4f20\u5165\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u5b83\u662f\u54cd\u5e94\u5f0f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>toRefs<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u54cd\u5e94\u5f0f\u53d8\u91cf\u7684\u666e\u901a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>context<\/strong>: \u4e00\u4e2a\u666e\u901a\u7684JavaScript\u5bf9\u8c61\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2aattrs\u5bf9\u8c61\u3001slots\u3001emit\u51fd\u6570\u7b49\uff0c\u7528\u4e8e\u7ec4\u4ef6\u4e4b\u95f4\u7684\u901a\u4fe1\u548c\u63d2\u69fd\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u54cd\u5e94\u5f0f\u6570\u636e\u7684\u521b\u5efa\u4e0e\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u54cd\u5e94\u5f0f\u6570\u636e\u662fsetup\u51fd\u6570\u4e2d\u6700\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u3002\u901a\u8fc7\u4f7f\u7528Vue3\u7684Composition API\u4e2d\u7684 <code>reactive<\/code>\u3001<code>ref<\/code>\u3001<code>computed<\/code> \u7b49\u51fd\u6570\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5b9a\u4e49\u548c\u7ba1\u7406\u72b6\u6001\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong><code>reactive<\/code><\/strong>: \u5b83\u63a5\u53d7\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61\u5e76\u8fd4\u56de\u8be5\u54cd\u5e94\u5f0f\u7684\u4ee3\u7406\u5bf9\u8c61\u3002\u5b83\u9002\u5408\u7528\u4e8e\u58f0\u660e\u7ec4\u4ef6\u7684\u54cd\u5e94\u5f0f\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>ref<\/code><\/strong>: \u7528\u6765\u521b\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u5f15\u7528\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u5305\u88c5\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u800c\u4e14\u53ef\u4ee5\u88ab\u89e3\u6784\u4e14\u4fdd\u6301\u54cd\u5e94\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u7ec4\u5408\u5f0fAPI\u7684\u903b\u8f91\u590d\u7528<\/h3>\n<\/p>\n<p><p>\u5f53\u7ec4\u4ef6\u5f00\u59cb\u53d8\u5f97\u590d\u6742\uff0c\u903b\u8f91\u590d\u7528\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002setup\u51fd\u6570\u901a\u8fc7\u4f7f\u7528Composition API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u903b\u8f91\u5206\u9694\u6210\u53ef\u590d\u7528\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u903b\u8f91\u62bd\u8c61<\/strong>: \u51fd\u6570\u53ef\u4ee5\u5bfc\u51fa\u4e3a\u5355\u72ec\u7684\u6587\u4ef6\uff0c\u5e76\u53ef\u4ee5\u5728\u591a\u4e2a\u7ec4\u4ef6\u4e4b\u95f4\u5171\u4eab\u548c\u590d\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>provide<\/code> \u548c <code>inject<\/code><\/strong>: \u8fd9\u5bf9\u51fd\u6570\u7528\u4e8e\u8de8\u7ec4\u4ef6\u5c42\u7ea7\u63d0\u4f9b\u548c\u6ce8\u5165\u6570\u636e\uff0c\u6709\u52a9\u4e8e\u9700\u8981\u5171\u4eab\u72b6\u6001\u7684\u6df1\u5c42\u6b21\u7ec4\u4ef6\u901a\u4fe1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u64cd\u4f5c\u751f\u547d\u5468\u671f\u94a9\u5b50<\/h3>\n<\/p>\n<p><p>Vue3\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684composition API\u6765\u64cd\u4f5c\u751f\u547d\u5468\u671f\u94a9\u5b50\uff0c\u8fd9\u4e9b\u51fd\u6570\u4ee5 <code>on<\/code> \u5f00\u5934\uff0c\u5982 <code>onMounted<\/code>\u3001<code>onUpdated<\/code> \u548c <code>onUnmounted<\/code>\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong><code>onMounted<\/code><\/strong>: \u5728\u7ec4\u4ef6\u6302\u8f7d\u5230DOM\u540e\u6267\u884c\u7684\u94a9\u5b50\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>onUnmounted<\/code><\/strong>: \u5728\u7ec4\u4ef6\u5378\u8f7d\u524d\u6267\u884c\u7684\u94a9\u5b50\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8bbf\u95ee\u7ec4\u4ef6\u7684\u5c5e\u6027\u3001\u69fd\u548c\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728setup\u51fd\u6570\u4e2d\uff0c\u501f\u52a9context\u53c2\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u5230\u7ec4\u4ef6\u7684\u5c5e\u6027\u3001\u63d2\u69fd\u548c\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u69fd (<code>slots<\/code>)<\/strong>: context\u53c2\u6570\u4e2d\u63d0\u4f9b\u7684slots\u5bf9\u8c61\u5305\u542b\u4e86\u6240\u6709\u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u63d0\u4f9b\u7684\u63d2\u69fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e8b\u4ef6 (<code>emit<\/code>)<\/strong>: emit\u51fd\u6570\u7528\u4e8e\u89e6\u53d1\u4e8b\u4ef6\uff0c\u4ee5\u4fbf\u901a\u77e5\u7236\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4e0e\u6a21\u677f\u7684\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>setup\u51fd\u6570\u8fd4\u56de\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5728\u7ec4\u4ef6\u7684\u6a21\u677f\u4e2d\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8fd4\u56de\u54cd\u5e94\u5f0f\u72b6\u6001<\/strong>: \u5c06setup\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u72b6\u6001\u6216\u65b9\u6cd5\u8fd4\u56de\uff0c\u4f7f\u5f97\u5b83\u4eec\u5728\u6a21\u677f\u4e2d\u53ef\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>ref<\/code> \u8fd4\u56de\u503c<\/strong>: \u5f53\u8fd4\u56de\u503c\u6765\u81ea\u4e00\u4e2a<code>ref<\/code>, \u6a21\u677f\u5c06\u81ea\u52a8\u89e3\u6784\u5176\u5305\u542b\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u5b9e\u8df5\u5efa\u8bae\u548c\u5e38\u89c1\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5efa\u8bae\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u547d\u540d\u7ea6\u5b9a<\/strong>: \u4f8b\u5982\u4f7f\u7528 <code>use<\/code> \u524d\u7f00\u6765\u547d\u540d\u53ef\u590d\u7528\u7684\u7ec4\u5408\u5f0f\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u5316<\/strong>: \u5c3d\u53ef\u80fd\u5730\u5c06\u903b\u8f91\u5212\u5206\u4e3a\u6a21\u5757\uff0c\u4ee5facilitate code reuse\u548cm<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntainability\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u867d\u7136setup\u51fd\u6570\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u540c\u6837\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5173\u952e\u70b9\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u54cd\u5e94\u5f0f\u5f15\u7528\u4f20\u9012<\/strong>: \u5bf9\u4e8e\u5f15\u7528\u4f20\u5165\u5b50\u7ec4\u4ef6\u7684\u54cd\u5e94\u5f0f\u5bf9\u8c61\u8981\u7279\u522b\u5c0f\u5fc3\uff0c\u4ee5\u907f\u514d\u7834\u574f\u5176\u54cd\u5e94\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u89e3\u6784props<\/strong>: props\u662f\u54cd\u5e94\u5f0f\u7684\uff0c\u76f4\u63a5\u89e3\u6784\u53ef\u80fd\u4f1a\u5931\u53bb\u5176\u54cd\u5e94\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u603b\u4e4b\uff0csetup\u51fd\u6570\u662fVue3\u4e2d\u65b0\u5f15\u5165\u7684\u4e00\u4e2a\u91cd\u8981\u529f\u80fd\uff0c\u5b83\u63d0\u4f9b\u4e86\u6784\u5efa\u3001\u7ec4\u7ec7\u548c\u7ba1\u7406Vue\u7ec4\u4ef6\u6240\u9700\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u529f\u80fd\u3002\u901a\u8fc7\u7406\u89e3\u5e76\u5408\u7406\u4f7f\u7528setup\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528Vue3\u7684Composition API\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7ec4\u7ec7\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f Vue3 \u7684 setup \u51fd\u6570\uff1f<\/strong><\/p>\n<p>Vue3 \u7684 setup \u51fd\u6570\u662f\u5728\u7ec4\u4ef6\u521b\u5efa\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\u4e00\u4e2a\u65b0\u7684\u9009\u9879\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u66ff\u4ee3 Vue2 \u4e2d\u7684 data\u3001methods \u548c computed \u5c5e\u6027\uff0c\u7528\u4e8e\u8bbe\u7f6e\u7ec4\u4ef6\u7684\u72b6\u6001\u548c\u884c\u4e3a\u3002\u901a\u8fc7 setup \u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Vue3 \u7684 Composition API \u6765\u7f16\u5199\u66f4\u7075\u6d3b\u548c\u53ef\u7ec4\u5408\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528 Vue3 \u7684 setup \u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u5728\u4e00\u4e2a Vue3 \u7ec4\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec4\u4ef6\u9009\u9879\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a setup \u7684\u51fd\u6570\u6765\u4f7f\u7528 setup \u51fd\u6570\u3002\u5728 setup \u51fd\u6570\u5185\u90e8\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5230\u7ec4\u4ef6\u7684 props\u3001context \u548c attrs \u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 Vue3 \u7684\u54cd\u5e94\u5f0f\u51fd\u6570\uff08\u5982 ref\u3001reactive\uff09\u6765\u5b9a\u4e49\u7ec4\u4ef6\u7684\u72b6\u6001\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 ref \u51fd\u6570\u6765\u5b9a\u4e49\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u72b6\u6001\uff1a<\/p>\n<pre><code>import { ref } from &#039;vue&#039;;\n\nexport default {\n  setup() {\n    const count = ref(0);\n\n    \/\/ \u5176\u4ed6\u903b\u8f91...\n\n    return {\n      count,\n      \/\/ \u5176\u4ed6\u8fd4\u56de\u503c...\n    };\n  },\n};\n<\/code><\/pre>\n<p><strong>3. \u5728 Vue3 \u7684 setup \u51fd\u6570\u4e2d\u53ef\u4ee5\u6267\u884c\u54ea\u4e9b\u903b\u8f91\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5728 setup \u51fd\u6570\u4e2d\uff0c\u9664\u4e86\u5b9a\u4e49\u54cd\u5e94\u5f0f\u72b6\u6001\u4e4b\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6267\u884c\u5176\u4ed6\u7684\u903b\u8f91\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 watch \u51fd\u6570\u6765\u76d1\u542c\u4e00\u4e2a\u54cd\u5e94\u5f0f\u72b6\u6001\u7684\u53d8\u5316\uff0c\u6216\u8005\u4f7f\u7528 computed \u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u6d3e\u751f\u7684\u503c\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5728 setup \u51fd\u6570\u4e2d\u4f7f\u7528\u5176\u4ed6\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u8c03\u7528 API \u8bf7\u6c42\u6570\u636e\uff0c\u751a\u81f3\u4f7f\u7528\u5176\u4ed6\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5904\u7406\u4e1a\u52a1\u903b\u8f91\u3002setup \u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u548c\u6269\u5c55\u6027\u5f3a\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u8ba9\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u590d\u7528\u4f60\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Vue3 \u9879\u76ee\u4e2d\u7684 setup \u51fd\u6570\u662f\u7ec4\u4ef6\u9009\u9879API\u7684\u66ff\u4ee3\uff0c\u7528\u6765\u5728\u7ec4\u4ef6\u521b\u5efa\u4e4b\u524d\u6267\u884c\u54cd\u5e94\u5f0f\u72b6\u6001\u7684\u58f0\u660e\u3001\u751f\u547d\u5468\u671f\u94a9 [&hellip;]","protected":false},"author":3,"featured_media":299409,"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\/299393"}],"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=299393"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299409"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}