{"id":273497,"date":"2024-05-16T13:40:36","date_gmt":"2024-05-16T05:40:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/273497.html"},"modified":"2024-05-16T13:40:48","modified_gmt":"2024-05-16T05:40:48","slug":"vue2-%e9%a1%b9%e7%9b%ae%e7%bb%84%e4%bb%b6-tree-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%97%a0%e9%99%90%e7%ba%a7%e6%a0%91%e5%bd%a2%e8%8f%9c%e5%8d%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/273497.html","title":{"rendered":"Vue2 \u9879\u76ee\u7ec4\u4ef6 tree \u5982\u4f55\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195351\/37927858-0e42-42a2-9fe8-c7bad2965692.webp\" alt=\"Vue2 \u9879\u76ee\u7ec4\u4ef6 tree \u5982\u4f55\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\" \/><\/p>\n<p><p>\u5728Vue2\u9879\u76ee\u4e2d\uff0c\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u53ef\u4ee5\u901a\u8fc7<strong>\u9012\u5f52\u7ec4\u4ef6\u3001Vuex\u72b6\u6001\u7ba1\u7406\u3001Prop\u4f20\u503c<\/strong>\u3001\u4ee5\u53ca<strong>\u4e8b\u4ef6\u76d1\u542c<\/strong>\u6765\u5b8c\u6210\u3002\u4f7f\u7528\u9012\u5f52\u7ec4\u4ef6\u662f\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7684\u5173\u952e\u6240\u5728\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u4f7f\u5f97\u540c\u4e00\u4e2a\u7ec4\u4ef6\u5728\u81ea\u8eab\u5185\u90e8\u88ab\u53cd\u590d\u8c03\u7528\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u5c42\u7ea7\u7ed3\u6784\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u9012\u5f52\u7ec4\u4ef6<\/strong>\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u672a\u77e5\u5c42\u7ea7\u7684\u6570\u636e\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5730\u521b\u5efa\u51fa\u52a8\u6001\u7684\u6811\u5f62\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9012\u5f52\u7ec4\u4ef6\u7684\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u7ec4\u4ef6\u7684\u5b9e\u73b0\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u57fa\u7840\u7ec4\u4ef6\uff0c\u8be5\u7ec4\u4ef6\u5728template\u90e8\u5206\u8c03\u7528\u81ea\u5df1\uff0c\u5b9e\u73b0\u81ea\u6211\u5f15\u7528\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u6811\u5f62\u83dc\u5355\u8fd9\u6837\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u6811\u5f62\u83dc\u5355\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\u672c\u8d28\u4e0a\u662f\u76f8\u540c\u7684\uff0c\u53ea\u662f\u6570\u636e\u4e0d\u540c\u800c\u5df2\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5728Vue\u7ec4\u4ef6\u4e2d\u58f0\u660e\u81ea\u8eab\u7ec4\u4ef6\u540d\u79f0\uff0c\u4f7f\u7528<code>name<\/code>\u5c5e\u6027\u3002<\/li>\n<li>\u7136\u540e\uff0c\u5728\u6a21\u677f\u4e2d\u901a\u8fc7<code>v-for<\/code>\u904d\u5386\u8282\u70b9\uff0c\u5e76\u5728\u9700\u8981\u65f6\u901a\u8fc7\u7ec4\u4ef6\u81ea\u6211\u8c03\u7528\u6765\u6e32\u67d3\u5b50\u8282\u70b9\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">&lt;template&gt;<\/p>\n<p>  &lt;div&gt;<\/p>\n<p>    &lt;div v-for=&quot;item in treeData&quot; :key=&quot;item.id&quot;&gt;<\/p>\n<p>      &lt;div&gt;{{ item.name }}&lt;\/div&gt;<\/p>\n<p>      &lt;tree-menu v-if=&quot;item.children &amp;&amp; item.children.length&quot; :tree-data=&quot;item.children&quot;\/&gt;<\/p>\n<p>    &lt;\/div&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>export default {<\/p>\n<p>  name: &#039;TreeMenu&#039;,<\/p>\n<p>  props: [&#039;treeData&#039;]<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Vuex\u72b6\u6001\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5229\u7528Vuex\u53ef\u4ee5\u5b9e\u73b0\u8de8\u7ec4\u4ef6\u7684\u72b6\u6001\u5171\u4eab\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u6811\u5f62\u83dc\u5355\uff0c\u53ef\u80fd\u9700\u8981\u5728\u4e0d\u540c\u7684\u7ec4\u4ef6\u4e4b\u95f4\u5171\u4eab\u67d0\u4e9b\u72b6\u6001\uff0c\u6bd4\u5982\u5f53\u524d\u9009\u4e2d\u7684\u8282\u70b9\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728Vuex\u4e2d\u5b9a\u4e49\u6811\u5f62\u83dc\u5355\u7684\u72b6\u6001\uff0c\u5305\u62ec\u8282\u70b9\u6570\u636e\u3001\u9009\u4e2d\u8282\u70b9\u7b49\u3002<\/li>\n<li>\u901a\u8fc7mutations\u6216actions\u6765\u66f4\u65b0\u8fd9\u4e9b\u72b6\u6001\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u54cd\u5e94\u5f0f\u548c\u4e00\u81f4\u6027\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001Prop\u4f20\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u9012\u5f52\u7ec4\u4ef6\u4e2d\uff0c\u4e0a\u5c42\u7ec4\u4ef6\u5230\u5b50\u7ec4\u4ef6\u7684\u6570\u636e\u4f20\u9012\u662f\u901a\u8fc7Prop\u6765\u5b8c\u6210\u7684\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u53ef\u4ee5\u5c06\u6bcf\u4e00\u5c42\u7684\u6570\u636e\u4f5c\u4e3aProp\u4f20\u7ed9\u4e0b\u4e00\u5c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7236\u7ec4\u4ef6\u901a\u8fc7props\u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u6570\u636e\u3002<\/li>\n<li>\u5b50\u7ec4\u4ef6\u63a5\u6536\u7236\u7ec4\u4ef6\u4f20\u9012\u7684props\uff0c\u5e76\u4e14\u5728\u9700\u8981\u65f6\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u9012\u5f52\u8c03\u7528\u7684\u81ea\u8eab\u7ec4\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4e8b\u4ef6\u76d1\u542c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7528\u6237\u4ea4\u4e92\uff0c\u5982\u70b9\u51fb\u8282\u70b9\u6269\u5c55\u6216\u6536\u7f29\u5b50\u83dc\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728\u6bcf\u4e2a\u8282\u70b9\u4e0a\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5f53\u70b9\u51fb\u65f6\uff0c\u6539\u53d8\u8be5\u8282\u70b9\u7684\u5c55\u5f00\u6216\u6536\u7f29\u72b6\u6001\u3002<\/li>\n<li>\u53ef\u4ee5\u901a\u8fc7<code>$emit<\/code>\u6765\u89e6\u53d1\u4e8b\u4ef6\uff0c\u5e76\u5728\u7236\u7ec4\u4ef6\u4e2d\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u4ea4\u4e92\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u3001\u7528\u6237\u4ea4\u4e92\u53cb\u597d\u7684\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u3002\u8fd9\u4e0d\u4ec5\u589e\u5f3a\u4e86\u5e94\u7528\u7684\u53ef\u7528\u6027\uff0c\u4e5f\u63d0\u5347\u4e86\u5f00\u53d1\u7684\u7075\u6d3b\u6027\u4e0e\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Vue2\u9879\u76ee\u4e2d\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\uff1f<\/strong><br \/>\n\u5728Vue2\u9879\u76ee\u4e2d\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u7ec4\u4ef6\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aTreeMenu\u7684\u7ec4\u4ef6\u3002\u7136\u540e\uff0c\u5728\u7ec4\u4ef6\u5185\u90e8\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u8c03\u7528\u6765\u6e32\u67d3\u6811\u5f62\u83dc\u5355\u7684\u5b50\u83dc\u5355\u3002\u901a\u8fc7\u4f20\u9012\u6570\u636e\u548c\u4f7f\u7528v-for\u6307\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u4e3a\u6bcf\u4e2a\u5b50\u83dc\u5355\u751f\u6210\u76f8\u5e94\u7684\u5b50\u7ec4\u4ef6\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u65e0\u8bba\u83dc\u5355\u6709\u591a\u5c11\u5c42\u7ea7\uff0c\u90fd\u53ef\u4ee5\u6b63\u786e\u5730\u6e32\u67d3\u51fa\u6765\u3002<\/p>\n<p><strong>2. Vue2\u9879\u76ee\u4e2d\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\u7684\u6ce8\u610f\u4e8b\u9879\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\n\u5728\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u5728\u8bbe\u8ba1\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6bcf\u4e2a\u83dc\u5355\u5bf9\u8c61\u90fd\u5305\u542b\u5176\u5b50\u83dc\u5355\u7684\u5c5e\u6027\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u9012\u5f52\u7ec4\u4ef6\u4e2d\u8fdb\u884c\u904d\u5386\u548c\u6e32\u67d3\u3002<\/li>\n<li>\u5728\u8fdb\u884c\u9012\u5f52\u6e32\u67d3\u65f6\uff0c\u8981\u6ce8\u610f\u5408\u7406\u8bbe\u7f6e\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u9012\u5f52\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<li>\u8981\u5408\u7406\u5904\u7406\u83dc\u5355\u7684\u5c55\u5f00\u4e0e\u6298\u53e0\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u503c\u5c5e\u6027\u6765\u63a7\u5236\u5b50\u83dc\u5355\u7684\u663e\u793a\u4e0e\u9690\u85cf\u3002<\/li>\n<li>\u53ef\u4ee5\u4e3a\u83dc\u5355\u9879\u7ed1\u5b9a\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u4ee5\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\u7684\u4ea4\u4e92\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c55\u5f00\u4e0e\u6298\u53e0\u3001\u9009\u4e2d\u7b49\u3002<\/li>\n<\/ul>\n<p><strong>3. \u6709\u6ca1\u6709\u73b0\u6210\u7684Vue2\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\u53ef\u7528\uff1f<\/strong><br \/>\n\u662f\u7684\uff0cVue2\u793e\u533a\u6709\u5f88\u591a\u73b0\u6210\u7684\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u7ec4\u4ef6\u53ef\u4f9b\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u5f15\u64ce\u6216\u5728Vue\u7ec4\u4ef6\u5e93\u4e2d\u67e5\u627e\u8fd9\u4e9b\u7ec4\u4ef6\u3002\u6709\u4e9b\u7ec4\u4ef6\u5305\u542b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u6837\u5f0f\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u9879\u76ee\u7684\u9700\u6c42\u3002\u5728\u4f7f\u7528\u524d\uff0c\u4f60\u9700\u8981\u4ed4\u7ec6\u9605\u8bfb\u7ec4\u4ef6\u6587\u6863\uff0c\u5e76\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u914d\u7f6e\u548c\u8c03\u6574\u3002\u540c\u65f6\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u5728\u73b0\u6709\u7ec4\u4ef6\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4fee\u6539\u548c\u5b9a\u5236\uff0c\u4ee5\u6ee1\u8db3\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Vue2\u9879\u76ee\u4e2d\uff0c\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u5f62\u83dc\u5355\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u7ec4\u4ef6\u3001Vuex\u72b6\u6001\u7ba1\u7406\u3001Prop\u4f20\u503c\u3001\u4ee5\u53ca\u4e8b\u4ef6\u76d1\u542c\u6765\u5b8c\u6210\u3002\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":273512,"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\/273497"}],"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=273497"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/273497\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/273512"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=273497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=273497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=273497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}