{"id":1651,"date":"2024-11-10T12:22:57","date_gmt":"2024-11-10T04:22:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1651\/"},"modified":"2024-11-10T12:22:57","modified_gmt":"2024-11-10T04:22:57","slug":"%e5%9c%a8-vue-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%9c%a8%e7%88%b6%e5%ad%90%e7%bb%84%e4%bb%b6%e9%97%b4%e4%bc%a0%e9%80%92-map-%e5%8f%98%e9%87%8f%e5%b9%b6%e9%81%bf%e5%85%8d-typeerror-cannot-read-pr","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1651\/","title":{"rendered":"\u5728 Vue \u4e2d\uff0c\u5982\u4f55\u5728\u7236\u5b50\u7ec4\u4ef6\u95f4\u4f20\u9012 Map \u53d8\u91cf\u5e76\u907f\u514d &#8220;TypeError: Cannot read properties of undefined (reading &#8216;get&#8217;)&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173052642937149.jpg\" class=\"aligncenter\" title=\"\u5728 Vue \u4e2d\uff0c\u5982\u4f55\u5728\u7236\u5b50\u7ec4\u4ef6\u95f4\u4f20\u9012 Map \u53d8\u91cf\u5e76\u907f\u514d &#8220;TypeError: Cannot read properties of undefined (reading &#8216;get&#8217;)&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"\u5728 Vue \u4e2d\uff0c\u5982\u4f55\u5728\u7236\u5b50\u7ec4\u4ef6\u95f4\u4f20\u9012 Map \u53d8\u91cf\u5e76\u907f\u514d &#8220;TypeError: Cannot read properties of undefined (reading &#8216;get&#8217;)&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728  \u4e2d\u5982\u4f55\u5728 return \u4e2d\u5b9a\u4e49 map \u7c7b\u578b\u7684\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u5728 Vue \u4e2d\uff0creturn \u4e2d\u5b9a\u4e49\u4e00\u4e2a map \u7c7b\u578b\u7684\u53d8\u91cf\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>return {\n  roomPictureList: new Map()\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f46\u662f\uff0c\u5728\u5c06\u7236\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u4f20\u9012 map \u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u9519\u8bef\u3002\u5f53\u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u4e00\u4e2a map \u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u6536\u5230\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>[Vue warn]: Error in render: \"TypeError: Cannot read properties of undefined (reading 'get')\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\u6b64\u9519\u8bef\uff1f<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u9700\u8981\u786e\u4fdd map \u53d8\u91cf\u5728\u7236\u7ec4\u4ef6\u4e2d\u5df2\u521d\u59cb\u5316\u3002\u5426\u5219\uff0c\u5f53\u5b50\u7ec4\u4ef6\u5c1d\u8bd5\u8bbf\u95ee map \u4e2d\u7684\u67d0\u4e2a\u9879\u65f6\uff0c\u5b83\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a roomPictureList \u7684 map \u53d8\u91cf\uff1a<\/p>\n<pre>\/\/ \u7236\u7ec4\u4ef6\nexport default {\n  data() {\n    return {\n      roomPictureList: new Map()\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff1a<\/p>\n<pre>\/\/ \u5b50\u7ec4\u4ef6\nexport default {\n  props: ['roomPictureList'],\n  render() {\n    return &lt;div&gt;{this.roomPictureList.get('key')}&lt;\/div&gt;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u95ee\u9898\u51fa\u73b0\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u5f53\u5b50\u7ec4\u4ef6\u5c1d\u8bd5\u8bbf\u95ee key \u65f6\uff0croomPictureList \u5c1a\u672a\u521d\u59cb\u5316\u4e3a new Map()\u3002\u56e0\u6b64\uff0croomPictureList.get(&#8216;key&#8217;) \u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u5c06 roomPictureList \u7684\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a null\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/\/ \u7236\u7ec4\u4ef6\nexport default {\n  data() {\n    return {\n      roomPictureList: null\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5f53\u5b50\u7ec4\u4ef6\u8bbf\u95ee roomPictureList.get(&#8216;key&#8217;) \u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de null \u800c\u4e0d\u662f\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728 Vue \u4e2d\uff0c\u5982\u4f55\u5728\u7236\u5b50\u7ec4\u4ef6\u95f4\u4f20\u9012 Map \u53d8\u91cf\u5e76\u907f\u514d &#8220;TypeError: Cannot read properties of undefined (reading &#8216;get&#8217;)&#8221; \u9519\u8bef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 \u4e2d\u5982\u4f55\u5728 return \u4e2d\u5b9a\u4e49 map \u7c7b\u578b\u7684\u53d8\u91cf\uff1f \u5728 Vue \u4e2d\uff0creturn \u4e2d\u5b9a\u4e49\u4e00\u4e2a map \u7c7b\u578b\u7684\u53d8\u91cf\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a return { roomPictureList: new Map() } \u767b\u5f55\u540e\u590d\u5236 \u4f46\u662f\uff0c\u5728\u5c06\u7236\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u4f20\u9012 map \u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u9519\u8bef\u3002\u5f53\u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u4e00\u4e2a map \u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u6536\u5230\u4ee5\u4e0b\u9519\u8bef\uff1a [Vue warn]: Error in render: &#8220;TypeError: Cannot read properties of undefined (reading &#8216;get&#8217;)&#8221; \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u89e3\u51b3\u6b64\u9519\u8bef\uff1f \u8981\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u9700\u8981\u786e\u4fdd map \u53d8\u91cf\u5728\u7236\u7ec4\u4ef6\u4e2d\u5df2\u521d\u59cb\u5316\u3002\u5426\u5219\uff0c\u5f53\u5b50\u7ec4\u4ef6\u5c1d\u8bd5\u8bbf\u95ee map \u4e2d\u7684\u67d0\u4e2a\u9879\u65f6\uff0c\u5b83\u4f1a\u629b\u51fa\u9519\u8bef\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a roomPictureList \u7684 map \u53d8\u91cf\uff1a \/\/ \u7236\u7ec4\u4ef6 export default { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}