{"id":7867,"date":"2023-11-28T20:03:17","date_gmt":"2023-11-28T12:03:17","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7867"},"modified":"2023-12-01T23:22:26","modified_gmt":"2023-12-01T15:22:26","slug":"vue-zhongnexttick-zhi-dao-ma-shi-xian-yuan-li-shi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/vue-zhongnexttick-zhi-dao-ma-shi-xian-yuan-li-shi\/","title":{"rendered":"Vue \u4e2d nextTick \u77e5\u9053\u5417\uff0c\u5b9e\u73b0\u539f\u7406\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><code>nextTick<\/code> \u662f Vue \u63d0\u4f9b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728 DOM \u66f4\u65b0\u961f\u5217\u88ab\u6e05\u7a7a\u4e4b\u540e\u6267\u884c\u5ef6\u8fdf\u56de\u8c03\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5728 Vue \u66f4\u65b0 DOM \u540e\uff0c\u7b49\u5f85 Vue \u5b8c\u6210\u66f4\u65b0\u540e\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u5e38\u7528\u4e8e\u5728 DOM \u53d8\u5316\u540e\u83b7\u53d6\u6700\u65b0\u7684 DOM \u7ed3\u6784\u6216\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002<\/p>\n<h3><a id=\"%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b9e\u73b0\u539f\u7406\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>Microtasks \u548c Macrotasks\uff1a<\/strong> JavaScript \u4e2d\u5b58\u5728\u4e24\u79cd\u4efb\u52a1\u961f\u5217\uff0cMicrotasks\uff08\u5fae\u4efb\u52a1\u961f\u5217\uff09\u548c Macrotasks\uff08\u5b8f\u4efb\u52a1\u961f\u5217\uff09\u3002Vue \u5728\u6267\u884c <code>nextTick<\/code> \u65f6\u4f1a\u5229\u7528\u5fae\u4efb\u52a1\u961f\u5217\u3002<\/p>\n<\/li>\n<li>\n<p><strong>DOM \u66f4\u65b0\u961f\u5217\uff1a<\/strong> \u5f53 Vue \u66f4\u65b0 DOM \u65f6\uff0c\u5b83\u4f1a\u5c06 DOM \u53d8\u66f4\u64cd\u4f5c\u653e\u5165\u4e00\u4e2a\u961f\u5217\u4e2d\uff0c\u5e76\u5728\u9002\u5f53\u7684\u65f6\u673a\u6e05\u7a7a\u961f\u5217\u5e76\u5c06\u53d8\u66f4\u5e94\u7528\u5230\u5b9e\u9645\u7684 DOM \u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>nextTick \u7684\u4f7f\u7528\uff1a<\/strong> \u5f53\u4f60\u8c03\u7528 <code>this.$nextTick(callback)<\/code> \u65f6\uff0cVue \u4f1a\u5c06\u8be5\u56de\u8c03\u51fd\u6570\u63a8\u5165\u4e00\u4e2a\u5fae\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u5728 DOM \u66f4\u65b0\u540e\u6267\u884c\u8fd9\u4e2a\u56de\u8c03\u3002\u8fd9\u610f\u5473\u7740\u56de\u8c03\u51fd\u6570\u4f1a\u5728 DOM \u66f4\u65b0\u5468\u671f\u7ed3\u675f\u540e\u7acb\u5373\u6267\u884c\uff0c\u800c\u4e0d\u4f1a\u7b49\u5230\u4e0b\u4e00\u4e2a JavaScript tick\u3002<\/p>\n<\/li>\n<li>\n<p><strong>Promise \u6216 MutationObserver\uff1a<\/strong> Vue \u5728\u4e0d\u540c\u73af\u5883\u4e0b\uff08\u6bd4\u5982\u6d4f\u89c8\u5668\u3001Node.js\uff09\u53ef\u80fd\u91c7\u7528\u4e0d\u540c\u7684\u5e95\u5c42\u673a\u5236\u6765\u5b9e\u73b0 <code>nextTick<\/code>\uff0c\u4f8b\u5982\u4f7f\u7528 Promise \u6216 MutationObserver\u3002<\/p>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u793a\u4f8b\uff1a<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u5728 Vue \u7ec4\u4ef6\u4e2d\u4f7f\u7528 nextTick\nthis.$nextTick(function() {\n  \/\/ \u5728 DOM \u66f4\u65b0\u540e\u6267\u884c\u7684\u64cd\u4f5c\n});\n<\/code><\/pre>\n<p><code>nextTick<\/code> \u7684\u6838\u5fc3\u4f5c\u7528\u662f\u8ba9\u4f60\u80fd\u591f\u5728\u4e0b\u6b21 DOM \u66f4\u65b0\u5faa\u73af\u7ed3\u675f\u4e4b\u540e\u6267\u884c\u5ef6\u8fdf\u56de\u8c03\uff0c\u4ee5\u786e\u4fdd\u5728\u64cd\u4f5c DOM \u5143\u7d20\u540e\u80fd\u591f\u83b7\u53d6\u5230\u6700\u65b0\u7684 DOM \u7ed3\u6784\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u79cd\u673a\u5236\u5728\u5904\u7406 Vue \u7684\u5f02\u6b65\u66f4\u65b0\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u5728\u66f4\u65b0\u4e4b\u540e\u7acb\u5373\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nextTick \u662f Vue \u63d0\u4f9b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728 DOM \u66f4\u65b0\u961f\u5217\u88ab\u6e05\u7a7a\u4e4b\u540e\u6267\u884c\u5ef6\u8fdf\u56de\u8c03\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5728 V [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-7867","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7867"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7867\/revisions"}],"predecessor-version":[{"id":13972,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7867\/revisions\/13972"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7867"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}