{"id":52365,"date":"2024-12-03T12:33:53","date_gmt":"2024-12-03T04:33:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52365\/"},"modified":"2024-12-03T12:33:53","modified_gmt":"2024-12-03T04:33:53","slug":"vue3-%e4%b8%ad-onload-%e6%96%b9%e6%b3%95%e4%b8%8d%e6%89%a7%e8%a1%8c%e7%9a%84%e5%8e%9f%e5%9b%a0%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52365\/","title":{"rendered":"Vue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Vue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f <\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aVue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f \u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732073463673d57f756645.jpg\" class=\"aligncenter\" title=\"Vue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"Vue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>Vue3 onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u5728 Vue3 \u4e2d\uff0c\u4f7f\u7528 onload \u65b9\u6cd5\u76d1\u542c\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3a onload \u4e8b\u4ef6\u53ea\u4f1a\u5728\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\uff0c\u5373\u5728 HTML \u6587\u6863\u52a0\u8f7d\u5b8c\u6210\u540e\u3002<\/p>\n<p><strong>\u573a\u666f\u8be6\u60c5\u53ca\u95ee\u9898<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6d4b\u8bd5\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>window.onload = () =&gt; {\n  console.log('loaded')\n}\nonMounted(() =&gt; {\n  console.log('mounted')\n  nextTick(() =&gt; {\n    console.log('nextTick');\n  })\n})<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmounted \u548c nextTick \u90fd\u4f1a\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u6267\u884c\uff0c\u4f46 onload \u4e0d\u4f1a\u6267\u884c\u3002\u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u52a0\u8f7d\u4e2d\u72b6\u6001\u5e76\u9700\u8981\u5728\u9875\u9762\u5b8c\u5168\u52a0\u8f7d\u540e\u5173\u95ed\u8be5\u72b6\u6001\u65f6\uff0c\u4f7f\u7528 onload \u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u56e0\u4e3a\u5728\u80cc\u666f\u56fe\u5b8c\u6210\u6e32\u67d3\u4e4b\u524d onload \u5c31\u5df2\u7ecf\u6267\u884c\u4e86\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528 Vue \u7ec4\u4ef6\u4e2d\u7684 $nextTick \u51fd\u6570\u3002$nextTick \u786e\u4fdd\u5728\u7ec4\u4ef6\u6302\u8f7d\u5b8c\u6210\u540e\u6267\u884c\u56de\u8c03\uff0c\u4ece\u800c\u907f\u514d\u5728 onload \u4e8b\u4ef6\u89e6\u53d1\u524d\u6267\u884c\u3002<\/p>\n<p><strong>\u6539\u8fdb\u7684\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>onMounted(() =&gt; {\n  nextTick(() =&gt; {\n    console.log('\u9875\u9762\u52a0\u8f7d\u5b8c\u6210')\n    \/\/ \u5173\u95ed\u52a0\u8f7d\u4e2d\u72b6\u6001\n  })\n})<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 $nextTick\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u7ec4\u4ef6\u6302\u8f7d\u5e76\u5b8c\u6210\u9875\u9762\u6e32\u67d3\u540e\u6267\u884c\u4ee3\u7801\uff0c\u4ece\u800c\u6ee1\u8db3\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\u5173\u95ed\u52a0\u8f7d\u4e2d\u72b6\u6001\u7684\u9700\u6c42\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aVue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f \u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win10\u79fb\u52a8\u786c\u76d8\u5728\u7535\u8111\u4e0a\u4e0d\u663e\u793a\u600e\u4e48\u529e\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Vue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aVue3 \u4e2d onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f \u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 Vue3 onload \u65b9\u6cd5\u4e0d\u6267\u884c\u7684\u539f\u56e0 \u5728 Vue3 \u4e2d\uff0c\u4f7f\u7528 onload \u65b9\u6cd5\u76d1\u542c\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u6267\u884c\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3a onload \u4e8b\u4ef6\u53ea\u4f1a\u5728\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6b21\uff0c\u5373\u5728 HTML \u6587\u6863\u52a0\u8f7d\u5b8c\u6210\u540e\u3002 \u573a\u666f\u8be6\u60c5\u53ca\u95ee\u9898 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u6d4b\u8bd5\u4ee3\u7801\u793a\u4f8b\uff1a window.onload = () =&gt; { console.log(&#8216;loaded&#8217;) } onMounted(() =&gt; { console.log(&#8216;mounted&#8217;) nextTick(() =&gt; { console.log(&#8216;nextTick&#8217;); }) }) \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmounted \u548c nextTick \u90fd\u4f1a\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u6267\u884c\uff0c\u4f46 onload \u4e0d\u4f1a\u6267\u884c\u3002\u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u52a0\u8f7d\u4e2d\u72b6\u6001\u5e76\u9700\u8981\u5728\u9875\u9762\u5b8c\u5168\u52a0\u8f7d\u540e\u5173\u95ed\u8be5\u72b6\u6001\u65f6\uff0c\u4f7f\u7528 onload \u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u56e0\u4e3a\u5728\u80cc\u666f\u56fe\u5b8c\u6210\u6e32\u67d3\u4e4b\u524d onload \u5c31\u5df2\u7ecf\u6267\u884c\u4e86\u3002 \u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528 Vue \u7ec4\u4ef6\u4e2d\u7684 $nextTick [&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-52365","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52365","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=52365"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52365\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}