{"id":441726,"date":"2025-05-28T09:42:30","date_gmt":"2025-05-28T01:42:30","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441726"},"modified":"2025-05-28T09:42:30","modified_gmt":"2025-05-28T01:42:30","slug":"%e9%b8%bf%e8%92%99%e5%bc%80%e5%8f%91%e5%ae%9e%e6%88%98%ef%bc%9aforeach%e5%be%aa%e7%8e%af%e4%b8%ad%e9%94%ae%e5%80%bc%e7%94%9f%e6%88%90%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441726\/","title":{"rendered":"\u9e3f\u8499\u5f00\u53d1\u5b9e\u6218\uff1aForEach\u5faa\u73af\u4e2d\u952e\u503c\u751f\u6210\u7684\u91cd\u8981\u6027\u89e3\u6790"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5728\u9e3f\u8499\u5f00\u53d1\u4e2d\uff0c\u8bf8\u5982 <code>List<\/code>\u3001<code>Grid<\/code> \u548c <code>WaterFlow<\/code> \u7b49\u7ec4\u4ef6\u5728\u5b9e\u73b0\u6570\u636e\u6e32\u67d3\u65f6\uff0c\u5f80\u5f80\u9700\u8981\u4f9d\u8d56 <code>ForEach<\/code> \u6216 <code>LazyForEach<\/code> \u8fdb\u884c\u5faa\u73af\u751f\u6210\u3002\u867d\u7136\u4f7f\u7528\u65b9\u5f0f\u770b\u4f3c\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5982\u679c\u5ffd\u7565\u952e\u503c\u751f\u6210\u51fd\u6570\u7684\u4f5c\u7528\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1\u4ee4\u4eba\u56f0\u60d1\u7684\u6e32\u67d3\u5f02\u5e38\u95ee\u9898\uff0c\u5f71\u54cd\u7528\u6237\u754c\u9762\u7684\u66f4\u65b0\u4e0e\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ece\u6280\u672f\u7ec6\u8282\u51fa\u53d1\uff0c\u6df1\u5165\u5256\u6790\u4e3a\u4f55\u5728\u9e3f\u8499UI\u5f00\u53d1\u4e2d\uff0c\u5fc5\u987b\u91cd\u89c6 <code>ForEach<\/code> \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u2014\u2014\u952e\u503c\u751f\u6210\u51fd\u6570\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u6848\u4f8b\u4e0e\u6700\u4f73\u5b9e\u8df5\u4e3a\u6280\u672f\u5f00\u53d1\u8005\u63d0\u4f9b\u53c2\u8003\u5efa\u8bae\u3002<\/p>\n<h2>\u6848\u4f8b\u5f15\u5165\uff1a\u6e32\u67d3\u201c\u6b63\u5e38\u201d\u5374\u85cf\u9690\u60a3<\/h2>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u7b80\u5355\u5b9e\u73b0\u4e00\u4e2a\u5217\u8868\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-ts\">List({ space: 20, initialIndex: 0 }) {\r\n  ForEach([\"\u6761\u76ee1\", \"\u6761\u76ee2\", \"\u6761\u76ee3\"], (item: string) =&gt; {\r\n    ListItem() {\r\n      Text(item)\r\n        .width('100%')\r\n        .height(50)\r\n        .fontSize(16)\r\n        .fontColor(Color.White)\r\n        .textAlign(TextAlign.Center)\r\n        .backgroundColor(Color.Orange)\r\n    }\r\n  }, (item: string) =&gt; item)\r\n}\r\n<\/code><\/pre>\n<p>\u521d\u770b\u65e0\u4efb\u4f55\u95ee\u9898\uff0c\u4f46\u5f53\u6211\u4eec\u5c06\u6570\u636e\u6e90\u4ece\u9759\u6001\u6570\u7ec4\u8f6c\u4e3a\u52a8\u6001\u53ef\u53d8\u6570\u7ec4\u5e76\u901a\u8fc7\u6309\u94ae\u8ffd\u52a0\u5185\u5bb9\u65f6\uff0c\u95ee\u9898\u4fbf\u4f1a\u66b4\u9732\u51fa\u6765\uff1a<\/p>\n<pre><code class=\"language-ts\">@State list: string[] = [\"\u6761\u76ee1\", \"\u6761\u76ee2\", \"\u6761\u76ee3\"];\r\n\r\nbuild() {\r\n  Column() {\r\n    Button(\"\u8ffd\u52a0\u6570\u636e\").onClick(() =&gt; {\r\n      this.list.push(\"\u6761\u76ee3\")\r\n    })\r\n    List({ space: 20, initialIndex: 0 }) {\r\n      ForEach(this.list, (item: string) =&gt; {\r\n        ListItem() {\r\n          Text(item)\r\n            .width('100%')\r\n            .height(50)\r\n            .fontSize(16)\r\n            .fontColor(Color.White)\r\n            .textAlign(TextAlign.Center)\r\n            .backgroundColor(Color.Orange)\r\n        }\r\n      }, (item: string) =&gt; item)\r\n    }\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>\u867d\u7136\u8ffd\u52a0\u6309\u94ae\u88ab\u591a\u6b21\u70b9\u51fb\uff0c\u4f46\u5728UI\u4e2d\u201c\u6761\u76ee3\u201d\u5e76\u672a\u91cd\u590d\u6e32\u67d3\uff0c\u8fd9\u6b63\u662f\u952e\u503c\u91cd\u590d\u9020\u6210\u7684\u95ee\u9898\u3002\u7cfb\u7edf\u8bc6\u522b\u5230\u76f8\u540c\u7684\u952e\u503c\uff0c\u4e0d\u518d\u521b\u5efa\u65b0\u7ec4\u4ef6\uff0c\u5bfc\u81f4\u754c\u9762\u6e32\u67d3\u975e\u9884\u671f\u3002<\/p>\n<h2>ForEach\u7684\u4e09\u4e2a\u5173\u952e\u53c2\u6570\u89e3\u6790<\/h2>\n<p>\u7406\u89e3 <code>ForEach<\/code> \u7684\u51fd\u6570\u7b7e\u540d\u81f3\u5173\u91cd\u8981\uff1a<\/p>\n<pre><code class=\"language-ts\">ForEach(arr: Array&lt;any&gt;, itemGenerator: (item, index) =&gt; void, keyGenerator?: (item, index) =&gt; string)\r\n<\/code><\/pre>\n<ul>\n<li><strong>\u6570\u636e\u6e90\uff08arr\uff09<\/strong>\uff1a\u51b3\u5b9a\u5faa\u73af\u7684\u5185\u5bb9\u6570\u91cf\uff1b<\/li>\n<li><strong>\u7ec4\u4ef6\u751f\u6210\u51fd\u6570\uff08itemGenerator\uff09<\/strong>\uff1a\u4e3a\u6bcf\u9879\u6570\u636e\u751f\u6210\u89c6\u56fe\uff1b<\/li>\n<li><strong>\u952e\u503c\u751f\u6210\u51fd\u6570\uff08keyGenerator\uff09<\/strong>\uff1a\u786e\u4fdd\u6bcf\u9879\u6e32\u67d3\u5bf9\u5e94\u552f\u4e00\u7ec4\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u7cfb\u7edf\u9ed8\u8ba4\u952e\u503c\u89c4\u5219\u4e3a <code>(index + '__' + JSON.stringify(item))<\/code>\u3002\u4f46\u82e5\u6570\u636e\u91cd\u590d\u6216\u7d22\u5f15\u503c\u76f8\u540c\uff0c\u4fbf\u4f1a\u9020\u6210\u952e\u503c\u51b2\u7a81\uff0c\u6700\u7ec8\u5bfc\u81f4\u65e0\u6cd5\u6b63\u786e\u6e32\u67d3\u3002<\/p>\n<h2>\u6b63\u786e\u4f7f\u7528\u952e\u503c\u7684\u5f00\u53d1\u5efa\u8bae<\/h2>\n<p>\u4e3a\u907f\u514d UI \u6e32\u67d3\u5f02\u5e38\uff0c\u4ee5\u4e0b\u51e0\u70b9\u503c\u5f97\u7279\u522b\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li><strong>\u786e\u4fdd\u952e\u503c\u552f\u4e00\u6027<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528\u4e1a\u52a1\u4e2d\u5177\u6709\u552f\u4e00\u6027\u7684\u5b57\u6bb5\uff0c\u5982\u5bf9\u8c61\u7684 <code>id<\/code>\uff1b<\/li>\n<li><strong>\u907f\u514d\u4f7f\u7528 index \u4f5c\u4e3a\u952e\u503c<\/strong>\uff1a\u56e0\u5176\u4e0d\u7a33\u5b9a\uff0c\u4fee\u6539\u6570\u636e\u6e90\u7ed3\u6784\u53ef\u80fd\u7834\u574f\u6e32\u67d3\u987a\u5e8f\uff1b<\/li>\n<li><strong>\u4e0d\u8981\u7701\u7565 keyGenerator \u53c2\u6570<\/strong>\uff1a\u5c24\u5176\u5728\u4f7f\u7528\u57fa\u7840\u7c7b\u578b\u6570\u7ec4\u65f6\uff0c\u5982\u5b57\u7b26\u4e32\u6570\u7ec4\uff1b<\/li>\n<li><strong>\u907f\u514d\u6df7\u7528 ForEach \u4e0e LazyForEach<\/strong>\uff1a\u5b98\u65b9\u5e76\u4e0d\u63a8\u8350\u8fd9\u6837\u505a\uff0c\u53ef\u80fd\u5e26\u6765\u96be\u4ee5\u6392\u67e5\u7684\u6027\u80fd\u4e0e\u6e32\u67d3\u95ee\u9898\u3002<\/li>\n<\/ol>\n<h2>\u5b9e\u6218\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p>\u5728\u9999\u6e2fvps\u3001\u4e91\u8ba1\u7b97\u5e73\u53f0\u548c\u9ad8\u6027\u80fd\u67b6\u6784\u4e2d\uff0cUI\u6e32\u67d3\u6027\u80fd\u76f4\u63a5\u5f71\u54cd\u524d\u7aef\u4f53\u9a8c\uff0c\u7279\u522b\u662f\u5728\u6d77\u91cf\u6570\u636e\u573a\u666f\u4e0b\u5c24\u4e3a\u5173\u952e\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e <code>ForEach<\/code> \u7684\u952e\u503c\u751f\u6210\u51fd\u6570\uff0c\u4e0d\u4ec5\u80fd\u786e\u4fdd\u754c\u9762\u6e32\u67d3\u7684\u6b63\u786e\u6027\uff0c\u8fd8\u80fd\u6709\u6548\u964d\u4f4e\u7ec4\u4ef6\u91cd\u590d\u6e32\u67d3\u5e26\u6765\u7684\u6027\u80fd\u635f\u8017\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u4ea7\u54c1\u7684\u54cd\u5e94\u6548\u7387\u3002<\/p>\n<p>\u9009\u62e9\u4e00\u4e2a\u53ef\u9760\u7684\u670d\u52a1\u5668\u5e73\u53f0\u540c\u6837\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u90e8\u7f72\u9e3f\u8499App\u6216\u9700\u8981\u627f\u8f7d\u9ad8\u5e76\u53d1\u7684\u5e94\u7528\u73af\u5883\u4e2d\u3002\u6211\u4eec\u63a8\u8350\u9009\u62e9\u5177\u5907\u9ad8\u901f\u7f51\u7edc\u548c\u7a33\u5b9a\u6027\u80fd\u7684 <strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u7075\u6d3b\u3001\u53ef\u62d3\u5c55\u7684\u90e8\u7f72\u65b9\u6848\u3002\u65e0\u8bba\u662f\u5f00\u53d1\u6d4b\u8bd5\u8fd8\u662f\u6b63\u5f0f\u4e0a\u7ebf\uff0c<a href=\"https:\/\/idc.net\/\"><strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong><\/a> \u90fd\u80fd\u4e3a\u60a8\u6784\u5efa\u5f3a\u52b2\u540e\u76fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u524d\u8a00 \u5728\u9e3f\u8499\u5f00\u53d1\u4e2d\uff0c\u8bf8\u5982 List\u3001Grid \u548c WaterFlow \u7b49\u7ec4\u4ef6\u5728\u5b9e\u73b0\u6570\u636e\u6e32\u67d3\u65f6\uff0c\u5f80 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[188588],"tags":[],"class_list":["post-441726","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441726"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441726\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}