{"id":6541,"date":"2022-07-20T15:23:34","date_gmt":"2022-07-20T07:23:34","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6541"},"modified":"2022-07-20T15:23:34","modified_gmt":"2022-07-20T07:23:34","slug":"vue-%e5%8e%9f%e7%90%86%e6%95%b4%e7%90%86","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6541","title":{"rendered":"Vue \u539f\u7406\u6574\u7406"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/gxll499294075\/article\/details\/123667632<\/p>\n\n\n\n<p><strong>1. \u7ec4\u4ef6\u5316\u57fa\u7840=&gt;\uff08<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=MVVM&amp;spm=1001.2101.3001.7020\" target=\"_blank\">MVVM<\/a>\u6a21\u578b\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4f20\u7edf\u7ec4\u4ef6\uff0c\u77e5\u8bc6\u9759\u6001\u6e32\u67d3\uff0c\u66f4\u65b0\u4f9d\u8d56\u4e8e\u64cd\u4f5cDOM\u3002<\/p>\n\n\n\n<p>Vue\u7684\u6838\u5fc3\u7406\u5ff5\u662f<strong>\u6570\u636e\u9a71\u52a8<\/strong>\u7684\u7406\u5ff5\uff0c\u6240\u8c13\u7684\u6570\u636e\u9a71\u52a8\u7684\u7406\u5ff5\uff1a\u5f53\u6570\u636e\u53d1\u751f\u53d8\u5316\u7684\u65f6\u5019\uff0c\u7528\u6237\u754c\u9762\u4e5f\u4f1a\u53d1\u751f\u76f8\u5e94\u7684\u53d8\u5316\uff0c\u5f00\u53d1\u8005\u5e76\u4e0d\u9700\u8981\u624b\u52a8\u7684\u53bb\u4fee\u6539<code>dom\u3002<\/code><\/p>\n\n\n\n<p>\u4f18\u70b9\uff1a\u4e0d\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u53bb\u9891\u7e41\u7684\u64cd\u4f5c<code>dom<\/code>\u4e86\uff0c\u8fd9\u6837\u63d0\u9ad8\u4e86\u5f00\u53d1\u7684\u6548\u7387\uff0c\u540c\u65f6\u4e5f\u907f\u514d\u4e86\u5728\u64cd\u4f5c<code>Dom<\/code>\u7684\u65f6\u5019\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p><code>Vue.js<\/code>\u7684\u6570\u636e\u9a71\u52a8\u662f\u901a\u8fc7<code>MVVM<\/code>\u8fd9\u79cd<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E6%A1%86%E6%9E%B6&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u6846\u67b6<\/a>\u6765\u5b9e\u73b0\u7684\uff0c<code>MVVM<\/code> \u6846\u67b6\u4e3b\u8981\u5305\u542b\u4e09\u90e8\u5206\uff1a<code>Model<\/code>, <code>View<\/code>, <code>ViewMode<\/code><\/p>\n\n\n\n<p>\u6570\u636e\u9a71\u52a8\u89c6\u56fe &#8211; Vue MVVM<\/p>\n\n\n\n<p>MVVM\u662fModel-View-ViewModel\u7f29\u5199\uff0c\u4e5f\u5c31\u662f\u628aMVC\u4e2d\u7684Controller\u6f14\u53d8\u6210ViewModel\u3002Model\u4ee3\u8868\u6570\u636e\u6a21\u578b\uff0cView\u4ee3\u8868UI\u7ec4\u4ef6\uff0cViewModel\u662fView\u548cModel\u5c42\u7684\u6865\u6881\uff0c\u6570\u636e\u4f1a\u7ed1\u5b9a\u5230ViewModel\u5c42\u5e76\u81ea\u52a8\u5c06\u6570\u636e\u6e32\u67d3\u5230\u9875\u9762\u4e2d\uff0c\u89c6\u56fe\u53d8\u5316\u7684\u65f6\u5019\u901a\u77e5viewModel\u5c42\u66f4\u65b0\u6570\u636e\u3002<\/p>\n\n\n\n<p><strong>2. Vue\u7684\u54cd\u5e94\u5f0f\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-1024x499.png\" alt=\"\" class=\"wp-image-6543\" width=\"521\" height=\"254\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-1024x499.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-300x146.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-768x374.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-830x404.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-230x112.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-350x170.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206-480x234.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-206.png 1142w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/figure><\/div>\n\n\n\n<p><strong>&nbsp;\u6838\u5fc3\u5b9e\u73b0\u7c7b:<\/strong><\/p>\n\n\n\n<ul><li>Observer : \u5b83\u7684\u4f5c\u7528\u662f\u7ed9\u5bf9\u8c61\u7684\u5c5e\u6027\u6dfb\u52a0 getter \u548c setter\uff0c\u7528\u4e8e\u4f9d\u8d56\u6536\u96c6\u548c\u6d3e\u53d1\u66f4\u65b0\u3002<\/li><li>Dep : \u7528\u4e8e\u6536\u96c6\u5f53\u524d\u54cd\u5e94\u5f0f\u5bf9\u8c61\u7684\u4f9d\u8d56\u5173\u7cfb,\u6bcf\u4e2a\u54cd\u5e94\u5f0f\u5bf9\u8c61\u5305\u62ec\u5b50\u5bf9\u8c61\u90fd\u62e5\u6709\u4e00\u4e2a Dep \u5b9e\u4f8b\uff08\u91cc\u9762 subs \u662f Watcher \u5b9e\u4f8b\u6570\u7ec4\uff09,\u5f53\u6570\u636e\u6709\u53d8\u66f4\u65f6,\u4f1a\u901a\u8fc7 dep.notify()\u901a\u77e5\u5404\u4e2a watcher\u3002<\/li><li>Watcher : \u89c2\u5bdf\u8005\u5bf9\u8c61 , \u5b9e\u4f8b\u5206\u4e3a\u6e32\u67d3 watcher (render watcher),\u8ba1\u7b97\u5c5e\u6027 watcher (computed watcher),\u4fa6\u542c\u5668 watcher\uff08user watcher\uff09\u4e09\u79cd<\/li><\/ul>\n\n\n\n<p>Watcher \u548c Dep \u7684\u5173\u7cfb\uff1awatcher \u4e2d\u5b9e\u4f8b\u5316\u4e86 dep \u5e76\u5411 dep.subs \u4e2d\u6dfb\u52a0\u4e86\u8ba2\u9605\u8005,dep \u901a\u8fc7 notify \u904d\u5386\u4e86 dep.subs \u901a\u77e5\u6bcf\u4e2a watcher \u66f4\u65b0\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-1024x273.png\" alt=\"\" class=\"wp-image-6544\" width=\"637\" height=\"169\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-1024x273.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-300x80.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-768x205.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-830x222.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-230x61.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-350x93.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207-480x128.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-207.png 1244w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-1024x233.png\" alt=\"\" class=\"wp-image-6545\" width=\"631\" height=\"143\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-1024x233.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-300x68.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-768x175.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-830x189.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-230x52.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-350x80.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208-480x109.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-208.png 1266w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-1024x139.png\" alt=\"\" class=\"wp-image-6546\" width=\"633\" height=\"86\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-1024x139.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-300x41.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-768x105.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-830x113.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-230x31.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-350x48.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209-480x65.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-209.png 1264w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/figure><\/div>\n\n\n\n<p><strong>Object.defineProperty \u5b9e\u73b0\u54cd\u5e94\u5f0f<\/strong>:<\/p>\n\n\n\n<ul><li>\u76d1\u542c\u5bf9\u8c61\uff0c\u76d1\u542c\u6570\u7ec4<\/li><li>\u590d\u6742\u5bf9\u8c61\uff0c\u6df1\u5ea6\u76d1\u542c<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const obj = {};\nconst data = {};\nconst name = 'zhangsan';\nObject.defineProperty(data, 'name', {\n    get: function () {\n        console.log('get');\n        return name;\n    },\n    set: function (newVal) {\n        console.log('set');\n        obj.name = newVal;\n    }\n})\nconsole.log(data.name);\ndata.name = 'lisi';\nconsole.log(obj.name);  \n<\/pre>\n\n\n\n<p><strong>Object.defineProperty \u7684\u7f3a\u70b9<\/strong>:<\/p>\n\n\n\n<ul><li>\u6df1\u5ea6\u76d1\u542c\u9700\u8981\u9012\u5f52\u5230\u5e95\uff0c\u4e00\u6b21\u6027\u8ba1\u7b97\u91cf\u5927<\/li><li>\u65e0\u6cd5\u76d1\u542c\u65b0\u589e\u5c5e\u6027\u3001\u5220\u9664\u5c5e\u6027\uff08\u8981\u4f7f\u7528 Vue.set Vue.delete\uff09<\/li><li>\u65e0\u6cd5\u539f\u751f\u76d1\u542c\u6570\u7ec4\uff0c\u9700\u8981\u7279\u6b8a\u5904\u7406\u3010\u5bf9\u6570\u7ec4\u7684\u65b9\u6cd5\u91cd\u5199\uff0c[&#8216;push&#8217;, &#8216;pop&#8217;, &#8216;shift&#8217;, &#8216;unshift&#8217;, &#8216;splice&#8217;,&#8217;sort&#8217;,&#8217;reverse&#8217;] \u8fd9\u51e0\u4e2a\u65b9\u6cd5\u66f4\u6539\u6570\u7ec4\u624d\u4f1a\u54cd\u5e94\u5f0f\u53d8\u5316\uff0c\u76f4\u63a5\u66f4\u6539\u7d22\u5f15\u4e0d\u4f1a\u54cd\u5e94\u5f0f\u6539\u53d8\u3011<\/li><\/ul>\n\n\n\n<p><strong>\u5bf9\u6570\u7ec4\u7684\u7279\u6b8a\u5904\u7406\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u89e6\u53d1\u66f4\u65b0\u89c6\u56fe\nfunction updateView() {\n    console.log('\u89c6\u56fe\u66f4\u65b0')\n}\n \n\/\/ \u91cd\u65b0\u5b9a\u4e49\u6570\u7ec4\u539f\u578b\nconst oldArrayProperty = Array.prototype\n\/\/ \u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u539f\u578b\u6307\u5411 oldArrayProperty \uff0c\u518d\u6269\u5c55\u65b0\u7684\u65b9\u6cd5\u4e0d\u4f1a\u5f71\u54cd\u539f\u578b\nconst arrProto = Object.create(oldArrayProperty);\n['push', 'pop', 'shift', 'unshift', 'splice'].forEach(methodName => {\n    arrProto[methodName] = function () {\n        updateView() \/\/ \u89e6\u53d1\u89c6\u56fe\u66f4\u65b0\n        oldArrayProperty[methodName].call(this, ...arguments)\n        \/\/ Array.prototype.push.call(this, ...arguments)\n    }\n})\n \n\/\/ \u91cd\u65b0\u5b9a\u4e49\u5c5e\u6027\uff0c\u76d1\u542c\u8d77\u6765\nfunction defineReactive(target, key, value) {\n    \/\/ \u6df1\u5ea6\u76d1\u542c\n    observer(value)\n \n    \/\/ \u6838\u5fc3 API\n    Object.defineProperty(target, key, {\n        get() {\n            return value\n        },\n        set(newValue) {\n            if (newValue !== value) {\n                \/\/ \u6df1\u5ea6\u76d1\u542c\n                observer(newValue)\n \n                \/\/ \u8bbe\u7f6e\u65b0\u503c\n                \/\/ \u6ce8\u610f\uff0cvalue \u4e00\u76f4\u5728\u95ed\u5305\u4e2d\uff0c\u6b64\u5904\u8bbe\u7f6e\u5b8c\u4e4b\u540e\uff0c\u518d get \u65f6\u4e5f\u662f\u4f1a\u83b7\u53d6\u6700\u65b0\u7684\u503c\n                value = newValue\n \n                \/\/ \u89e6\u53d1\u66f4\u65b0\u89c6\u56fe\n                updateView()\n            }\n        }\n    })\n}\n \n\/\/ \u76d1\u542c\u5bf9\u8c61\u5c5e\u6027\nfunction observer(target) {\n    if (typeof target !== 'object' || target === null) {\n        \/\/ \u4e0d\u662f\u5bf9\u8c61\u6216\u6570\u7ec4\n        return target\n    }\n \n    \/\/ \u6c61\u67d3\u5168\u5c40\u7684 Array \u539f\u578b\n    \/\/ Array.prototype.push = function () {\n    \/\/     updateView()\n    \/\/     ...\n    \/\/ }\n \n    if (Array.isArray(target)) {\n        target.__proto__ = arrProto\n    }\n \n    \/\/ \u91cd\u65b0\u5b9a\u4e49\u5404\u4e2a\u5c5e\u6027\uff08for in \u4e5f\u53ef\u4ee5\u904d\u5386\u6570\u7ec4\uff09\n    for (let key in target) {\n        defineReactive(target, key, target[key])\n    }\n}\n \n\/\/ \u51c6\u5907\u6570\u636e\nconst data = {\n    name: 'zhangsan',\n    age: 20,\n    info: {\n        address: '\u5317\u4eac' \/\/ \u9700\u8981\u6df1\u5ea6\u76d1\u542c\n    },\n    nums: [10, 20, 30]\n}\n \n\/\/ \u76d1\u542c\u6570\u636e\nobserver(data)\n \n\/\/ \u6d4b\u8bd5\n\/\/ data.name = 'lisi'\n\/\/ data.age = 21\n\/\/ \/\/ console.log('age', data.age)\n\/\/ data.x = '100' \/\/ \u65b0\u589e\u5c5e\u6027\uff0c\u76d1\u542c\u4e0d\u5230 \u2014\u2014 \u6240\u4ee5\u6709 Vue.set\n\/\/ delete data.name \/\/ \u5220\u9664\u5c5e\u6027\uff0c\u76d1\u542c\u4e0d\u5230 \u2014\u2014 \u6240\u6709\u5df2 Vue.delete\n\/\/ data.info.address = '\u4e0a\u6d77' \/\/ \u6df1\u5ea6\u76d1\u542c\ndata.nums.push(4) \/\/ \u76d1\u542c\u6570\u7ec4<\/pre>\n\n\n\n<p><strong>3.\u4e3a\u4ec0\u4e48\u5728 Vue3.0 \u91c7\u7528\u4e86 Proxy,\u629b\u5f03\u4e86 Object.defineProperty\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Object.defineProperty \u672c\u8eab\u6709\u4e00\u5b9a\u7684\u76d1\u63a7\u5230\u6570\u7ec4\u4e0b\u6807\u53d8\u5316\u7684\u80fd\u529b,\u4f46\u662f\u5728 Vue \u4e2d,\u4ece\u6027\u80fd\/\u4f53\u9a8c\u7684\u6027\u4ef7\u6bd4\u8003\u8651,\u5c24\u5927\u5927\u5c31\u5f03\u7528\u4e86\u8fd9\u4e2a\u7279\u6027(Vue \u4e3a\u4ec0\u4e48\u4e0d\u80fd\u68c0\u6d4b\u6570\u7ec4\u53d8\u52a8 )\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898,\u7ecf\u8fc7 vue \u5185\u90e8\u5904\u7406\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u76d1\u542c\u6570\u7ec4 [&#8216;push&#8217;, &#8216;pop&#8217;, &#8216;shift&#8217;, &#8216;unshift&#8217;, &#8216;splice&#8217;,&#8217;sort&#8217;,&#8217;reverse&#8217;]<\/p>\n\n\n\n<p>\u7531\u4e8e\u53ea\u9488\u5bf9\u4e86\u4ee5\u4e0a 7 \u79cd\u65b9\u6cd5\u8fdb\u884c\u4e86 hack \u5904\u7406,\u6240\u4ee5\u5176\u4ed6\u6570\u7ec4\u7684\u5c5e\u6027\u4e5f\u662f\u68c0\u6d4b\u4e0d\u5230\u7684,\u8fd8\u662f\u5177\u6709\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002<\/p>\n\n\n\n<p>Object.defineProperty \u53ea\u80fd\u52ab\u6301\u5bf9\u8c61\u7684\u5c5e\u6027,\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5bf9\u6bcf\u4e2a\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5c5e\u6027\u8fdb\u884c\u904d\u5386\u3002Vue 2.x \u91cc,\u662f\u901a\u8fc7 \u9012\u5f52 + \u904d\u5386 data \u5bf9\u8c61\u6765\u5b9e\u73b0\u5bf9\u6570\u636e\u7684\u76d1\u63a7\u7684,\u5982\u679c\u5c5e\u6027\u503c\u4e5f\u662f\u5bf9\u8c61\u90a3\u4e48\u9700\u8981\u6df1\u5ea6\u904d\u5386,\u663e\u7136\u5982\u679c\u80fd\u52ab\u6301\u4e00\u4e2a\u5b8c\u6574\u7684\u5bf9\u8c61\u662f\u624d\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">Proxy \u53ef\u4ee5\u52ab\u6301\u6574\u4e2a\u5bf9\u8c61,\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u3002Proxy \u4e0d\u4ec5\u53ef\u4ee5\u4ee3\u7406\u5bf9\u8c61,\u8fd8\u53ef\u4ee5\u4ee3\u7406\u6570\u7ec4\u3002\u8fd8\u53ef\u4ee5\u4ee3\u7406\u52a8\u6001\u589e\u52a0\u7684\u5c5e\u6027\u3002<\/span><\/p>\n\n\n\n<p><strong>4.computed \u7684\u5b9e\u73b0\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>computed \u672c\u8d28\u662f\u4e00\u4e2a<span class=\"has-inline-color has-vivid-red-color\">\u60f0\u6027\u6c42\u503c\u7684\u89c2\u5bdf\u8005\u3002<\/span><\/p>\n\n\n\n<p>computed \u5185\u90e8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u60f0\u6027\u7684 watcher,\u4e5f\u5c31\u662f computed watcher,computed watcher \u4e0d\u4f1a\u7acb\u523b\u6c42\u503c,\u540c\u65f6\u6301\u6709\u4e00\u4e2a dep \u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>\u5176\u5185\u90e8\u901a\u8fc7 this.dirty \u5c5e\u6027\u6807\u8bb0\u8ba1\u7b97\u5c5e\u6027\u662f\u5426\u9700\u8981\u91cd\u65b0\u6c42\u503c\u3002<\/p>\n\n\n\n<p>\u5f53 computed \u7684\u4f9d\u8d56\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6,\u5c31\u4f1a\u901a\u77e5\u8fd9\u4e2a\u60f0\u6027\u7684 watcher,<\/p>\n\n\n\n<p>computed watcher \u901a\u8fc7 this.dep.subs.length \u5224\u65ad\u6709\u6ca1\u6709\u8ba2\u9605\u8005,<\/p>\n\n\n\n<p>\u6709\u7684\u8bdd,\u4f1a\u91cd\u65b0\u8ba1\u7b97,\u7136\u540e\u5bf9\u6bd4\u65b0\u65e7\u503c,\u5982\u679c\u53d8\u5316\u4e86,\u4f1a\u91cd\u65b0\u6e32\u67d3\u3002 (Vue \u60f3\u786e\u4fdd\u4e0d\u4ec5\u4ec5\u662f\u8ba1\u7b97\u5c5e\u6027\u4f9d\u8d56\u7684\u503c\u53d1\u751f\u53d8\u5316\uff0c\u800c\u662f\u5f53\u8ba1\u7b97\u5c5e\u6027\u6700\u7ec8\u8ba1\u7b97\u7684\u503c\u53d1\u751f\u53d8\u5316\u65f6\u624d\u4f1a\u89e6\u53d1\u6e32\u67d3 watcher \u91cd\u65b0\u6e32\u67d3\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u4f18\u5316\u3002)<\/p>\n\n\n\n<p>\u6ca1\u6709\u7684\u8bdd,\u4ec5\u4ec5\u628a this.dirty = true\u3002 (\u5f53\u8ba1\u7b97\u5c5e\u6027\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6570\u636e\u65f6\uff0c\u5c5e\u6027\u5e76\u4e0d\u4f1a\u7acb\u5373\u91cd\u65b0\u8ba1\u7b97\uff0c\u53ea\u6709\u4e4b\u540e\u5176\u4ed6\u5730\u65b9\u9700\u8981\u8bfb\u53d6\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u5b83\u624d\u4f1a\u771f\u6b63\u8ba1\u7b97\uff0c\u5373\u5177\u5907 lazy\uff08\u61d2\u8ba1\u7b97\uff09\u7279\u6027\u3002)<\/p>\n\n\n\n<p><strong>5. computed \u548c watch \u6709\u4ec0\u4e48\u533a\u522b\u53ca\u8fd0\u7528\u573a\u666f?&nbsp;<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><br><strong>\u533a\u522b\uff1a<\/strong><\/p>\n\n\n\n<ul><li>computed \u8ba1\u7b97\u5c5e\u6027 : \u4f9d\u8d56\u5176\u5b83\u5c5e\u6027\u503c,\u5e76\u4e14 <strong>computed \u7684\u503c\u6709\u7f13\u5b58<\/strong>,\u53ea\u6709\u5b83\u4f9d\u8d56\u7684\u5c5e\u6027\u503c\u53d1\u751f\u6539\u53d8,\u4e0b\u4e00\u6b21\u83b7\u53d6 computed \u7684\u503c\u65f6\u624d\u4f1a\u91cd\u65b0\u8ba1\u7b97 computed \u7684\u503c\u3002<\/li><li>watch \u4fa6\u542c\u5668 : \u66f4\u591a\u7684\u662f\u300c\u89c2\u5bdf\u300d\u7684\u4f5c\u7528,<strong>\u65e0\u7f13\u5b58\u6027<\/strong>,\u7c7b\u4f3c\u4e8e\u67d0\u4e9b\u6570\u636e\u7684\u76d1\u542c\u56de\u8c03,\u6bcf\u5f53<strong>\u76d1\u542c\u7684\u6570\u636e\u53d8\u5316\u65f6\u90fd\u4f1a\u6267\u884c\u56de\u8c03\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c<\/strong>\u3002\u00a0<\/li><\/ul>\n\n\n\n<p><strong>\u8fd0\u7528\u573a\u666f<\/strong>:<\/p>\n\n\n\n<ul><li>\u5f53\u6211\u4eec\u9700\u8981\u8fdb\u884c\u6570\u503c\u8ba1\u7b97,\u5e76\u4e14\u4f9d\u8d56\u4e8e\u5176\u5b83\u6570\u636e\u65f6,\u5e94\u8be5\u4f7f\u7528 computed,\u56e0\u4e3a\u53ef\u4ee5\u5229\u7528 computed \u7684\u7f13\u5b58\u7279\u6027,\u907f\u514d\u6bcf\u6b21\u83b7\u53d6\u503c\u65f6,\u90fd\u8981\u91cd\u65b0\u8ba1\u7b97\u3002<\/li><li>\u5f53\u6211\u4eec\u9700\u8981\u5728<strong>\u6570\u636e\u53d8\u5316\u65f6\u6267\u884c\u5f02\u6b65\u6216\u5f00\u9500\u8f83\u5927\u7684\u64cd\u4f5c\u65f6,\u5e94\u8be5\u4f7f\u7528 watch<\/strong>,\u4f7f\u7528 watch \u9009\u9879\u5141\u8bb8\u6211\u4eec\u6267\u884c\u5f02\u6b65\u64cd\u4f5c ( \u8bbf\u95ee\u4e00\u4e2a API ),\u9650\u5236\u6211\u4eec\u6267\u884c\u8be5\u64cd\u4f5c\u7684\u9891\u7387,\u5e76\u5728\u6211\u4eec\u5f97\u5230\u6700\u7ec8\u7ed3\u679c\u524d,\u8bbe\u7f6e\u4e2d\u95f4\u72b6\u6001\u3002\u8fd9\u4e9b\u90fd\u662f\u8ba1\u7b97\u5c5e\u6027\u65e0\u6cd5\u505a\u5230\u7684\u3002<\/li><\/ul>\n\n\n\n<p><strong>6.\u00a0Vue \u4e2d\u7684 key \u5230\u5e95\u6709\u4ec0\u4e48\u7528\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>key \u662f\u7ed9\u6bcf\u4e00\u4e2a vnode \u7684\u552f\u4e00 id,\u4f9d\u9760 key,\u6211\u4eec\u7684 diff \u64cd\u4f5c\u53ef\u4ee5\u66f4\u51c6\u786e\u3001\u66f4\u5feb\u901f (\u5bf9\u4e8e\u7b80\u5355\u5217\u8868\u9875\u6e32\u67d3\u6765\u8bf4 diff \u8282\u70b9\u4e5f\u66f4\u5feb,\u4f46\u4f1a\u4ea7\u751f\u4e00\u4e9b\u9690\u85cf\u7684\u526f\u4f5c\u7528,\u6bd4\u5982\u53ef\u80fd\u4e0d\u4f1a\u4ea7\u751f\u8fc7\u6e21\u6548\u679c,\u6216\u8005\u5728\u67d0\u4e9b\u8282\u70b9\u6709\u7ed1\u5b9a\u6570\u636e\uff08\u8868\u5355\uff09\u72b6\u6001\uff0c\u4f1a\u51fa\u73b0\u72b6\u6001\u9519\u4f4d\u3002)<\/p>\n\n\n\n<p>diff \u7b97\u6cd5\u7684\u8fc7\u7a0b\u4e2d,\u5148\u4f1a\u8fdb\u884c\u65b0\u65e7\u8282\u70b9\u7684\u9996\u5c3e\u4ea4\u53c9\u5bf9\u6bd4,\u5f53\u65e0\u6cd5\u5339\u914d\u7684\u65f6\u5019\u4f1a\u7528\u65b0\u8282\u70b9\u7684 key \u4e0e\u65e7\u8282\u70b9\u8fdb\u884c\u6bd4\u5bf9,\u4ece\u800c\u627e\u5230\u76f8\u5e94\u65e7\u8282\u70b9.<\/p>\n\n\n\n<p>\u66f4\u51c6\u786e : <strong>\u56e0\u4e3a\u5e26 key \u5c31\u4e0d\u662f\u5c31\u5730\u590d\u7528\u4e86,<\/strong>\u5728 sameNode \u51fd\u6570 a.key === b.key \u5bf9\u6bd4\u4e2d\u53ef\u4ee5\u907f\u514d\u5c31\u5730\u590d\u7528\u7684\u60c5\u51b5\u3002\u6240\u4ee5\u4f1a\u66f4\u52a0\u51c6\u786e,\u5982\u679c\u4e0d\u52a0 key,\u4f1a\u5bfc\u81f4\u4e4b\u524d\u8282\u70b9\u7684\u72b6\u6001\u88ab\u4fdd\u7559\u4e0b\u6765,\u4f1a\u4ea7\u751f\u4e00\u7cfb\u5217\u7684 bug\u3002<\/p>\n\n\n\n<p>\u66f4\u5feb\u901f : key \u7684\u552f\u4e00\u6027\u53ef\u4ee5\u88ab Map \u6570\u636e\u7ed3\u6784\u5145\u5206\u5229\u7528,\u76f8\u6bd4\u4e8e\u904d\u5386\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6 O(n),Map \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ec5\u4ec5\u4e3a O(1),\u6e90\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function createKeyToOldIdx(children, beginIdx, endIdx) {\n \n  let i, key;\n \n  const map = {};\n \n  for (i = beginIdx; i &lt;= endIdx; ++i) {\n \n    key = children[i].key;\n \n    if (isDef(key)) map[key] = i;\n \n  }<\/pre>\n\n\n\n<p><strong>7.\u8c08\u4e00\u8c08 nextTick \u7684\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>JS \u8fd0\u884c\u673a\u5236<\/strong><\/p>\n\n\n\n<p>JS \u6267\u884c\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u5b83\u662f\u57fa\u4e8e\u4e8b\u4ef6\u5faa\u73af\u7684\u3002\u4e8b\u4ef6\u5faa\u73af\u5927\u81f4\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4:<\/p>\n\n\n\n<p>\u6240\u6709\u540c\u6b65\u4efb\u52a1\u90fd\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u5f62\u6210\u4e00\u4e2a\u6267\u884c\u6808\uff08execution context stack\uff09\u3002<br>\u4e3b\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u5b58\u5728\u4e00\u4e2a&#8221;\u4efb\u52a1\u961f\u5217&#8221;\uff08task queue\uff09\u3002\u53ea\u8981\u5f02\u6b65\u4efb\u52a1\u6709\u4e86\u8fd0\u884c\u7ed3\u679c\uff0c\u5c31\u5728&#8221;\u4efb\u52a1\u961f\u5217&#8221;\u4e4b\u4e2d\u653e\u7f6e\u4e00\u4e2a\u4e8b\u4ef6\u3002<br>\u4e00\u65e6&#8221;\u6267\u884c\u6808&#8221;\u4e2d\u7684\u6240\u6709\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u7cfb\u7edf\u5c31\u4f1a\u8bfb\u53d6&#8221;\u4efb\u52a1\u961f\u5217&#8221;\uff0c\u770b\u770b\u91cc\u9762\u6709\u54ea\u4e9b\u4e8b\u4ef6\u3002\u90a3\u4e9b\u5bf9\u5e94\u7684\u5f02\u6b65\u4efb\u52a1\uff0c\u4e8e\u662f\u7ed3\u675f\u7b49\u5f85\u72b6\u6001\uff0c\u8fdb\u5165\u6267\u884c\u6808\uff0c\u5f00\u59cb\u6267\u884c\u3002<br>\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u91cd\u590d\u4e0a\u9762\u7684\u7b2c\u4e09\u6b65\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-1024x688.png\" alt=\"\" class=\"wp-image-6557\" width=\"453\" height=\"304\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-1024x688.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-300x201.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-768x516.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-830x557.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-230x154.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-350x235.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213-480x322.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-213.png 1102w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/figure><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<p>\u4e3b\u7ebf\u7a0b\u7684\u6267\u884c\u8fc7\u7a0b\u5c31\u662f\u4e00\u4e2a tick\uff0c\u800c\u6240\u6709\u7684\u5f02\u6b65\u7ed3\u679c\u90fd\u662f\u901a\u8fc7 \u201c\u4efb\u52a1\u961f\u5217\u201d \u6765\u8c03\u5ea6\u3002 \u6d88\u606f\u961f\u5217\u4e2d\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u4e2a\u7684\u4efb\u52a1\uff08task\uff09\u3002 \u89c4\u8303\u4e2d\u89c4\u5b9a task \u5206\u4e3a\u4e24\u5927\u7c7b\uff0c\u5206\u522b\u662f macro task \u548c micro task\uff0c\u5e76\u4e14\u6bcf\u4e2a macro task \u7ed3\u675f\u540e\uff0c\u90fd\u8981\u6e05\u7a7a\u6240\u6709\u7684 micro task\u3002<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (macroTask of macroTaskQueue) {\n \n  \/\/ 1. Handle current MACRO-TASK\n \n  handleMacroTask();\n \n \n  \/\/ 2. Handle all MICRO-TASK\n \n  for (microTask of microTaskQueue) {\n \n    handleMicroTask(microTask);\n \n  }\n <\/pre>\n\n\n\n<p><strong>\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d :<\/strong><\/p>\n\n\n\n<ul><li>\u5e38\u89c1\u7684 macro task \u6709 setTimeout\u3001MessageChannel\u3001postMessage\u3001setImmediate<\/li><li>\u5e38\u89c1\u7684 micro task \u6709 MutationObsever \u548c Promise.then<\/li><\/ul>\n\n\n\n<p><strong>\u5f02\u6b65\u66f4\u65b0\u961f\u5217<\/strong>:<\/p>\n\n\n\n<p>\u53ef\u80fd\u4f60\u8fd8\u6ca1\u6709\u6ce8\u610f\u5230\uff0cVue \u5728\u66f4\u65b0 DOM \u65f6\u662f\u5f02\u6b65\u6267\u884c\u7684\u3002\u53ea\u8981\u4fa6\u542c\u5230\u6570\u636e\u53d8\u5316\uff0cVue \u5c06\u5f00\u542f\u4e00\u4e2a\u961f\u5217\uff0c\u5e76\u7f13\u51b2\u5728\u540c\u4e00\u4e8b\u4ef6\u5faa\u73af\u4e2d\u53d1\u751f\u7684\u6240\u6709\u6570\u636e\u53d8\u66f4\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u540c\u4e00\u4e2a watcher \u88ab\u591a\u6b21\u89e6\u53d1\uff0c\u53ea\u4f1a\u88ab\u63a8\u5165\u5230\u961f\u5217\u4e2d\u4e00\u6b21\u3002\u8fd9\u79cd\u5728\u7f13\u51b2\u65f6\u53bb\u9664\u91cd\u590d\u6570\u636e\u5bf9\u4e8e\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c DOM \u64cd\u4f5c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u5728\u4e0b\u4e00\u4e2a\u7684\u4e8b\u4ef6\u5faa\u73af\u201ctick\u201d\u4e2d\uff0cVue \u5237\u65b0\u961f\u5217\u5e76\u6267\u884c\u5b9e\u9645 (\u5df2\u53bb\u91cd\u7684) \u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>Vue \u5728\u5185\u90e8\u5bf9\u5f02\u6b65\u961f\u5217\u5c1d\u8bd5\u4f7f\u7528\u539f\u751f\u7684 Promise.then\u3001MutationObserver \u548c setImmediate\uff0c\u5982\u679c\u6267\u884c\u73af\u5883\u4e0d\u652f\u6301\uff0c\u5219\u4f1a\u91c7\u7528 setTimeout(fn, 0) \u4ee3\u66ff\u3002<\/p>\n\n\n\n<p>\u5728 vue2.5 \u7684\u6e90\u7801\u4e2d\uff0cmacrotask \u964d\u7ea7\u7684\u65b9\u6848\u4f9d\u6b21\u662f\uff1asetImmediate\u3001MessageChannel\u3001setTimeout<\/p>\n\n\n\n<p>vue \u7684 nextTick \u65b9\u6cd5\u7684\u5b9e\u73b0\u539f\u7406:<\/p>\n\n\n\n<p>vue \u7528\u5f02\u6b65\u961f\u5217\u7684\u65b9\u5f0f\u6765\u63a7\u5236 DOM \u66f4\u65b0\u548c nextTick \u56de\u8c03\u5148\u540e\u6267\u884c microtask \u56e0\u4e3a\u5176\u9ad8\u4f18\u5148\u7ea7\u7279\u6027\uff0c\u80fd\u786e\u4fdd\u961f\u5217\u4e2d\u7684\u5fae\u4efb\u52a1\u5728\u4e00\u6b21\u4e8b\u4ef6\u5faa\u73af\u524d\u88ab\u6267\u884c\u5b8c\u6bd5\u8003\u8651\u517c\u5bb9\u95ee\u9898,vue \u505a\u4e86 microtask \u5411 macrotask \u7684\u964d\u7ea7\u65b9\u6848<\/p>\n\n\n\n<p><strong>8.vue \u662f\u5982\u4f55\u5bf9\u6570\u7ec4\u65b9\u6cd5\u8fdb\u884c\u53d8\u5f02\u7684 ?\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6e90\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const arrayProto = Array.prototype;\nexport const arrayMethods = Object.create(arrayProto);\nconst methodsToPatch = [\"push\",\"pop\",\"shift\",\"unshift\",\"splice\",\"sort\",\"reverse\"];\n \n\/**\n * Intercept mutating methods and emit events\n *\/\n \nmethodsToPatch.forEach(function(method) {\n  \/\/ cache original method\n  const original = arrayProto[method];\n  def(arrayMethods, method, function mutator(...args) {\n \n    const result = original.apply(this, args);\n \n    const ob = this.__ob__;\n \n    let inserted;\n \n    switch (method) {\n \n      case \"push\":\n \n      case \"unshift\":\n \n        inserted = args;\n \n        break;\n \n      case \"splice\":\n \n        inserted = args.slice(2);\n \n        break;\n \n    }\n \n    if (inserted) ob.observeArray(inserted);\n \n    \/\/ notify change\n \n    ob.dep.notify();\n \n    return result;\n \n  });\n \n});\n \n\/**\n * Observe a list of Array items.\n *\/\n \nObserver.prototype.observeArray = function observeArray(items) {\n \n  for (var i = 0, l = items.length; i &lt; l; i++) {\n \n    observe(items[i]);\n \n  }\n \n};<\/pre>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4,<strong>Vue \u901a\u8fc7\u539f\u578b\u62e6\u622a\u7684\u65b9\u5f0f\u91cd\u5199\u4e86\u6570\u7ec4\u7684 7 \u4e2a\u65b9\u6cd5<\/strong>,\u9996\u5148\u83b7\u53d6\u5230\u8fd9\u4e2a\u6570\u7ec4\u7684ob,\u4e5f\u5c31\u662f\u5b83\u7684 Observer \u5bf9\u8c61,\u5982\u679c\u6709\u65b0\u7684\u503c,\u5c31\u8c03\u7528 observeArray \u5bf9\u65b0\u7684\u503c\u8fdb\u884c\u76d1\u542c,\u7136\u540e\u624b\u52a8\u8c03\u7528 <strong>notify<\/strong>,\u901a\u77e5 render watcher,\u6267\u884c update<\/p>\n\n\n\n<p><strong>9.Vue \u7ec4\u4ef6 data \u4e3a\u4ec0\u4e48\u5fc5\u987b\u662f\u51fd\u6570 ?\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>new Vue()\u5b9e\u4f8b\u4e2d,data \u53ef\u4ee5\u76f4\u63a5\u662f\u4e00\u4e2a\u5bf9\u8c61,\u4e3a\u4ec0\u4e48\u5728 vue \u7ec4\u4ef6\u4e2d,data \u5fc5\u987b\u662f\u4e00\u4e2a\u51fd\u6570\u5462?<\/p>\n\n\n\n<p>\u56e0\u4e3a\u7ec4\u4ef6\u662f\u53ef\u4ee5\u590d\u7528\u7684,JS \u91cc\u5bf9\u8c61\u662f\u5f15\u7528\u5173\u7cfb,\u5982\u679c\u7ec4\u4ef6 data \u662f\u4e00\u4e2a<strong>\u5bf9\u8c61<\/strong>,\u90a3\u4e48\u5b50\u7ec4\u4ef6\u4e2d\u7684 data \u5c5e\u6027\u503c<strong>\u4f1a\u4e92\u76f8\u6c61\u67d3,\u4ea7\u751f\u526f\u4f5c\u7528<\/strong>\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u4e00\u4e2a\u7ec4\u4ef6\u7684 d<strong>ata \u9009\u9879\u5fc5\u987b\u662f\u4e00\u4e2a\u51fd\u6570<\/strong>,\u56e0\u6b64\u6bcf\u4e2a\u5b9e\u4f8b\u53ef\u4ee5\u7ef4\u62a4\u4e00\u4efd\u88ab\u8fd4\u56de\u5bf9\u8c61\u7684<strong>\u72ec\u7acb\u7684\u62f7\u8d1d<\/strong>\u3002<strong>new Vue \u7684\u5b9e\u4f8b\u662f\u4e0d\u4f1a\u88ab\u590d\u7528\u7684,\u56e0\u6b64\u4e0d\u5b58\u5728\u4ee5\u4e0a\u95ee\u9898<\/strong>\u3002<\/p>\n\n\n\n<p><strong>10. \u8c08\u8c08 Vue \u4e8b\u4ef6\u673a\u5236, \u624b\u5199$on,$off,$emit,$once\u00a0<\/strong><\/p>\n\n\n\n<p>Vue \u4e8b\u4ef6\u673a\u5236 \u672c\u8d28\u4e0a\u5c31\u662f \u4e00\u4e2a \u53d1\u5e03-\u8ba2\u9605 \u6a21\u5f0f\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Vue {\n \n  constructor() {\n \n    \/\/  \u4e8b\u4ef6\u901a\u9053\u8c03\u5ea6\u4e2d\u5fc3\n \n    this._events = Object.create(null);\n \n  }\n \n  $on(event, fn) {\n \n    if (Array.isArray(event)) {\n \n      event.map(item => {\n \n        this.$on(item, fn);\n \n      });\n \n    } else {\n \n      (this._events[event] || (this._events[event] = [])).push(fn);\n \n    }\n \n    return this;\n \n  }\n \n  $once(event, fn) {\n \n    function on() {\n \n      this.$off(event, on);\n \n      fn.apply(this, arguments);\n \n    }\n \n    on.fn = fn;\n \n    this.$on(event, on);\n \n    return this;\n \n  }\n \n  $off(event, fn) {\n \n    if (!arguments.length) {\n \n      this._events = Object.create(null);\n \n      return this;\n \n    }\n \n    if (Array.isArray(event)) {\n \n      event.map(item => {\n \n        this.$off(item, fn);\n \n      });\n \n      return this;\n \n    }\n \n    const cbs = this._events[event];\n \n    if (!cbs) {\n \n      return this;\n \n    }\n \n    if (!fn) {\n \n      this._events[event] = null;\n \n      return this;\n \n    }\n \n    let cb;\n \n    let i = cbs.length;\n \n    while (i--) {\n \n      cb = cbs[i];\n \n      if (cb === fn || cb.fn === fn) {\n \n        cbs.splice(i, 1);\n \n        break;\n \n      }\n \n    }\n \n    return this;\n \n  }\n \n  $emit(event) {\n \n    let cbs = this._events[event];\n \n    if (cbs) {\n \n      const args = [].slice.call(arguments, 1);\n \n      cbs.map(item => {\n \n        args ? item.apply(this, args) : item.call(this);\n \n      });\n \n    }\n \n    return this;\n \n  }\n \n}\n<\/pre>\n\n\n\n<p><strong>11.\u8bf4\u8bf4 Vue \u7684\u6e32\u67d3\u8fc7\u7a0b<\/strong><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214.png\" alt=\"\" class=\"wp-image-6561\" width=\"443\" height=\"457\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214.png 890w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-291x300.png 291w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-768x792.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-830x856.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-230x237.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-350x361.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-214-480x495.png 480w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/figure><\/div>\n\n\n\n<p>\u00a0\u8c03\u7528 compile \u51fd\u6570,\u751f\u6210 render \u51fd\u6570\u5b57\u7b26\u4e32 ,\u7f16\u8bd1\u8fc7\u7a0b\u5982\u4e0b:<\/p>\n\n\n\n<ul><li>parse \u51fd\u6570\u89e3\u6790 template,\u751f\u6210 ast(\u62bd\u8c61\u8bed\u6cd5\u6811)<\/li><li>optimize \u51fd\u6570\u4f18\u5316\u9759\u6001\u8282\u70b9 (\u6807\u8bb0\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u66f4\u65b0\u7684\u5185\u5bb9,diff \u7b97\u6cd5\u4f1a\u76f4\u63a5\u8df3\u8fc7\u9759\u6001\u8282\u70b9,\u4ece\u800c\u51cf\u5c11\u6bd4\u8f83\u7684\u8fc7\u7a0b,\u4f18\u5316\u4e86 patch \u7684\u6027\u80fd)<\/li><li>generate \u51fd\u6570\u751f\u6210 render \u51fd\u6570\u5b57\u7b26\u4e32<\/li><\/ul>\n\n\n\n<p>\u8c03\u7528 new Watcher \u51fd\u6570,\u76d1\u542c\u6570\u636e\u7684\u53d8\u5316,\u5f53\u6570\u636e\u53d1\u751f\u53d8\u5316\u65f6\uff0cRender \u51fd\u6570\u6267\u884c\u751f\u6210 vnode \u5bf9\u8c61<\/p>\n\n\n\n<p>\u8c03\u7528 patch \u65b9\u6cd5,\u5bf9\u6bd4\u65b0\u65e7 vnode \u5bf9\u8c61,\u901a\u8fc7 DOM diff \u7b97\u6cd5,\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u771f\u6b63\u7684 DOM \u5143\u7d20<\/p>\n\n\n\n<p><strong>12.\u804a\u804a keep-alive \u7684\u5b9e\u73b0\u539f\u7406\u548c\u7f13\u5b58\u7b56\u7565\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export default {\n \n  name: \"keep-alive\",\n \n  abstract: true, \/\/ \u62bd\u8c61\u7ec4\u4ef6\u5c5e\u6027 ,\u5b83\u5728\u7ec4\u4ef6\u5b9e\u4f8b\u5efa\u7acb\u7236\u5b50\u5173\u7cfb\u7684\u65f6\u5019\u4f1a\u88ab\u5ffd\u7565,\u53d1\u751f\u5728 initLifecycle \u7684\u8fc7\u7a0b\u4e2d\n \n  props: {\n \n    include: patternTypes, \/\/ \u88ab\u7f13\u5b58\u7ec4\u4ef6\n \n    exclude: patternTypes, \/\/ \u4e0d\u88ab\u7f13\u5b58\u7ec4\u4ef6\n \n    max: [String, Number] \/\/ \u6307\u5b9a\u7f13\u5b58\u5927\u5c0f\n \n  },\n \n \n \n  created() {\n \n    this.cache = Object.create(null); \/\/ \u7f13\u5b58\n \n    this.keys = []; \/\/ \u7f13\u5b58\u7684VNode\u7684\u952e\n \n  },\n \n \n \n  destroyed() {\n \n    for (const key in this.cache) {\n \n      \/\/ \u5220\u9664\u6240\u6709\u7f13\u5b58\n \n      pruneCacheEntry(this.cache, key, this.keys);\n \n    }\n \n  },\n \n \n \n  mounted() {\n \n    \/\/ \u76d1\u542c\u7f13\u5b58\/\u4e0d\u7f13\u5b58\u7ec4\u4ef6\n \n    this.$watch(\"include\", val => {\n \n      pruneCache(this, name => matches(val, name));\n \n    });\n \n    this.$watch(\"exclude\", val => {\n \n      pruneCache(this, name => !matches(val, name));\n \n    });\n \n  },\n \n \n \n  render() {\n \n    \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5b50\u5143\u7d20\u7684 vnode\n \n    const slot = this.$slots.default;\n \n    const vnode: VNode = getFirstComponentChild(slot);\n \n    const componentOptions: ?VNodeComponentOptions =\n \n      vnode &amp;&amp; vnode.componentOptions;\n \n    if (componentOptions) {\n \n      \/\/ name\u4e0d\u5728inlcude\u4e2d\u6216\u8005\u5728exlude\u4e2d \u76f4\u63a5\u8fd4\u56devnode\n \n      \/\/ check pattern\n \n      const name: ?string = getComponentName(componentOptions);\n \n      const { include, exclude } = this;\n \n      if (\n \n        \/\/ not included\n \n        (include &amp;&amp; (!name || !matches(include, name))) ||\n \n        \/\/ excluded\n \n        (exclude &amp;&amp; name &amp;&amp; matches(exclude, name))\n \n      ) {\n \n        return vnode;\n \n      }\n \n \n \n      const { cache, keys } = this;\n \n      \/\/ \u83b7\u53d6\u952e\uff0c\u4f18\u5148\u83b7\u53d6\u7ec4\u4ef6\u7684name\u5b57\u6bb5\uff0c\u5426\u5219\u662f\u7ec4\u4ef6\u7684tag\n \n      const key: ?string =\n \n        vnode.key == null\n \n          ? \/\/ same constructor may get registered as different local components\n \n            \/\/ so cid alone is not enough (#3269)\n \n            componentOptions.Ctor.cid +\n \n            (componentOptions.tag ? `::${componentOptions.tag}` : \"\")\n \n          : vnode.key;\n \n      \/\/ \u547d\u4e2d\u7f13\u5b58,\u76f4\u63a5\u4ece\u7f13\u5b58\u62ffvnode \u7684\u7ec4\u4ef6\u5b9e\u4f8b,\u5e76\u4e14\u91cd\u65b0\u8c03\u6574\u4e86 key \u7684\u987a\u5e8f\u653e\u5728\u4e86\u6700\u540e\u4e00\u4e2a\n \n      if (cache[key]) {\n \n        vnode.componentInstance = cache[key].componentInstance;\n \n        \/\/ make current key freshest\n \n        remove(keys, key);\n \n        keys.push(key);\n \n      }\n \n      \/\/ \u4e0d\u547d\u4e2d\u7f13\u5b58,\u628a vnode \u8bbe\u7f6e\u8fdb\u7f13\u5b58\n \n      else {\n \n        cache[key] = vnode;\n \n        keys.push(key);\n \n        \/\/ prune oldest entry\n \n        \/\/ \u5982\u679c\u914d\u7f6e\u4e86 max \u5e76\u4e14\u7f13\u5b58\u7684\u957f\u5ea6\u8d85\u8fc7\u4e86 this.max\uff0c\u8fd8\u8981\u4ece\u7f13\u5b58\u4e2d\u5220\u9664\u7b2c\u4e00\u4e2a\n \n        if (this.max &amp;&amp; keys.length > parseInt(this.max)) {\n \n          pruneCacheEntry(cache, keys[0], keys, this._vnode);\n \n        }\n \n      }\n \n      \/\/ keepAlive\u6807\u8bb0\u4f4d\n \n      vnode.data.keepAlive = true;\n \n    }\n \n    return vnode || (slot &amp;&amp; slot[0]);\n \n  }\n \n};<\/pre>\n\n\n\n<p><strong>\u539f\u7406<\/strong><\/p>\n\n\n\n<p>\u83b7\u53d6 keep-alive \u5305\u88f9\u7740\u7684\u7b2c\u4e00\u4e2a\u5b50\u7ec4\u4ef6\u5bf9\u8c61\u53ca\u5176\u7ec4\u4ef6\u540d<br>\u6839\u636e\u8bbe\u5b9a\u7684 include\/exclude\uff08\u5982\u679c\u6709\uff09\u8fdb\u884c\u6761\u4ef6\u5339\u914d,\u51b3\u5b9a\u662f\u5426\u7f13\u5b58\u3002\u4e0d\u5339\u914d,\u76f4\u63a5\u8fd4\u56de\u7ec4\u4ef6\u5b9e\u4f8b<br>\u6839\u636e\u7ec4\u4ef6 ID \u548c tag \u751f\u6210\u7f13\u5b58 Key,\u5e76\u5728\u7f13\u5b58\u5bf9\u8c61\u4e2d\u67e5\u627e\u662f\u5426\u5df2\u7f13\u5b58\u8fc7\u8be5\u7ec4\u4ef6\u5b9e\u4f8b\u3002\u5982\u679c\u5b58\u5728,\u76f4\u63a5\u53d6\u51fa\u7f13\u5b58\u503c\u5e76\u66f4\u65b0\u8be5 key \u5728 this.keys \u4e2d\u7684\u4f4d\u7f6e(\u66f4\u65b0 key \u7684\u4f4d\u7f6e\u662f\u5b9e\u73b0 LRU \u7f6e\u6362\u7b56\u7565\u7684\u5173\u952e)<br>\u5728 this.cache \u5bf9\u8c61\u4e2d\u5b58\u50a8\u8be5\u7ec4\u4ef6\u5b9e\u4f8b\u5e76\u4fdd\u5b58 key \u503c,\u4e4b\u540e\u68c0\u67e5\u7f13\u5b58\u7684\u5b9e\u4f8b\u6570\u91cf\u662f\u5426\u8d85\u8fc7 max \u7684\u8bbe\u7f6e\u503c,\u8d85\u8fc7\u5219\u6839\u636e LRU \u7f6e\u6362\u7b56\u7565\u5220\u9664\u6700\u8fd1\u6700\u4e45\u672a\u4f7f\u7528\u7684\u5b9e\u4f8b\uff08\u5373\u662f\u4e0b\u6807\u4e3a 0 \u7684\u90a3\u4e2a key\uff09<br>\u6700\u540e\u7ec4\u4ef6\u5b9e\u4f8b\u7684 keepAlive \u5c5e\u6027\u8bbe\u7f6e\u4e3a true,\u8fd9\u4e2a\u5728\u6e32\u67d3\u548c\u6267\u884c\u88ab\u5305\u88f9\u7ec4\u4ef6\u7684\u94a9\u5b50\u51fd\u6570\u4f1a\u7528\u5230,\u8fd9\u91cc\u4e0d\u7ec6\u8bf4<\/p>\n\n\n\n<p><strong>LRU \u7f13\u5b58\u6dd8\u6c70\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>LRU\uff08Least recently used\uff09\u7b97\u6cd5\u6839\u636e\u6570\u636e\u7684\u5386\u53f2\u8bbf\u95ee\u8bb0\u5f55\u6765\u8fdb\u884c\u6dd8\u6c70\u6570\u636e,\u5176\u6838\u5fc3\u601d\u60f3\u662f\u201c\u5982\u679c\u6570\u636e\u6700\u8fd1\u88ab\u8bbf\u95ee\u8fc7,\u90a3\u4e48\u5c06\u6765\u88ab\u8bbf\u95ee\u7684\u51e0\u7387\u4e5f\u66f4\u9ad8\u201d\u3002\u00a0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-1024x490.png\" alt=\"\" class=\"wp-image-6563\" width=\"501\" height=\"239\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-1024x490.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-300x143.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-768x367.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-830x397.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-230x110.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-350x167.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215-480x230.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-215.png 1150w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n\n<p>keep-alive \u7684\u5b9e\u73b0\u6b63\u662f\u7528\u5230\u4e86 LRU \u7b56\u7565,\u5c06\u6700\u8fd1\u8bbf\u95ee\u7684\u7ec4\u4ef6 push \u5230 this.keys \u6700\u540e\u9762,this.keys[0]\u4e5f\u5c31\u662f\u6700\u4e45\u6ca1\u88ab\u8bbf\u95ee\u7684\u7ec4\u4ef6,\u5f53\u7f13\u5b58\u5b9e\u4f8b\u8d85\u8fc7 max \u8bbe\u7f6e\u503c,\u5220\u9664 this.keys[0]\u00a0<\/p>\n\n\n\n<p><strong>13. vm.$set()\u5b9e\u73b0\u539f\u7406\u662f\u4ec0\u4e48?\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5bf9\u4e8e\u3010\u68c0\u6d4b\u4e0d\u5230\u5bf9\u8c61\u5c5e\u6027\u7684\u6dfb\u52a0\u548c\u5220\u9664\u3011\u3010\u65e0\u6cd5\u76d1\u63a7\u5230\u6570\u7ec4\u4e0b\u6807\u7684\u53d8\u5316\u3011\u53ef\u4ee5\u7528 vue.$set\u3002<\/p>\n\n\n\n<p>\u53d7\u73b0\u4ee3 JavaScript \u7684\u9650\u5236 (\u800c\u4e14 Object.observe \u4e5f\u5df2\u7ecf\u88ab\u5e9f\u5f03)\uff0cVue \u65e0\u6cd5\u68c0\u6d4b\u5230\u5bf9\u8c61\u5c5e\u6027\u7684\u6dfb\u52a0\u6216\u5220\u9664\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e Vue \u4f1a\u5728\u521d\u59cb\u5316\u5b9e\u4f8b\u65f6\u5bf9\u5c5e\u6027\u6267\u884c getter\/setter \u8f6c\u5316\uff0c\u6240\u4ee5\u5c5e\u6027\u5fc5\u987b\u5728 data \u5bf9\u8c61\u4e0a\u5b58\u5728\u624d\u80fd\u8ba9 Vue \u5c06\u5b83\u8f6c\u6362\u4e3a\u54cd\u5e94\u5f0f\u7684\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u5df2\u7ecf\u521b\u5efa\u7684\u5b9e\u4f8b\uff0cVue \u4e0d\u5141\u8bb8\u52a8\u6001\u6dfb\u52a0\u6839\u7ea7\u522b\u7684\u54cd\u5e94\u5f0f\u5c5e\u6027\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u4f7f\u7528 Vue.set(object, propertyName, value) \u65b9\u6cd5\u5411\u5d4c\u5957\u5bf9\u8c61\u6dfb\u52a0\u54cd\u5e94\u5f0f\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48 Vue \u5185\u90e8\u662f\u5982\u4f55\u89e3\u51b3\u5bf9\u8c61\u65b0\u589e\u5c5e\u6027\u4e0d\u80fd\u54cd\u5e94\u7684\u95ee\u9898\u7684\u5462?<\/p>\n\n\n\n<p>\u5185\u90e8\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export function set(target: Array | Object, key: any, val: any): any {\n \n  \/\/ target \u4e3a\u6570\u7ec4\n \n  if (Array.isArray(target) &amp;&amp; isValidArrayIndex(key)) {\n \n    \/\/ \u4fee\u6539\u6570\u7ec4\u7684\u957f\u5ea6, \u907f\u514d\u7d22\u5f15>\u6570\u7ec4\u957f\u5ea6\u5bfc\u81f4splice()\u6267\u884c\u6709\u8bef\n \n    target.length = Math.max(target.length, key);\n \n    \/\/ \u5229\u7528\u6570\u7ec4\u7684splice\u53d8\u5f02\u65b9\u6cd5\u89e6\u53d1\u54cd\u5e94\u5f0f\n \n    target.splice(key, 1, val);\n \n    return val;\n \n  }\n \n  \/\/ target\u4e3a\u5bf9\u8c61, key\u5728target\u6216\u8005target.prototype\u4e0a \u4e14\u5fc5\u987b\u4e0d\u80fd\u5728 Object.prototype \u4e0a,\u76f4\u63a5\u8d4b\u503c\n \n  if (key in target &amp;&amp; !(key in Object.prototype)) {\n \n    target[key] = val;\n \n    return val;\n \n  }\n \n  \/\/ \u4ee5\u4e0a\u90fd\u4e0d\u6210\u7acb, \u5373\u5f00\u59cb\u7ed9target\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u5c5e\u6027\n \n  \/\/ \u83b7\u53d6Observer\u5b9e\u4f8b\n \n  const ob = (target: any).__ob__;\n \n  \/\/ target \u672c\u8eab\u5c31\u4e0d\u662f\u54cd\u5e94\u5f0f\u6570\u636e, \u76f4\u63a5\u8d4b\u503c\n \n  if (!ob) {\n \n    target[key] = val;\n \n    return val;\n \n  }\n \n  \/\/ \u8fdb\u884c\u54cd\u5e94\u5f0f\u5904\u7406\n \n  defineReactive(ob.value, key, val);\n \n  ob.dep.notify();\n \n  return val;\n \n}<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul><li>\u5982\u679c\u76ee\u6807\u662f\u6570\u7ec4,\u4f7f\u7528 vue \u5b9e\u73b0\u7684\u53d8\u5f02\u65b9\u6cd5 splice \u5b9e\u73b0\u54cd\u5e94\u5f0f<\/li><li>\u5982\u679c\u76ee\u6807\u662f\u5bf9\u8c61,\u5224\u65ad\u5c5e\u6027\u5b58\u5728,\u5373\u4e3a\u54cd\u5e94\u5f0f,\u76f4\u63a5\u8d4b\u503c<\/li><li>\u5982\u679c target \u672c\u8eab\u5c31\u4e0d\u662f\u54cd\u5e94\u5f0f,\u76f4\u63a5\u8d4b\u503c<\/li><li>\u5982\u679c\u5c5e\u6027\u4e0d\u662f\u54cd\u5e94\u5f0f,\u5219\u8c03\u7528 defineReactive \u65b9\u6cd5\u8fdb\u884c\u54cd\u5e94\u5f0f\u5904\u7406<\/li><\/ul>\n\n\n\n<p><strong>14.\u865a\u62dfDOM(vdom)\u548cdiff\u7b97\u6cd5\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>DOM\u64cd\u4f5c\u975e\u5e38\u8017\u8d39\u6027\u80fd<\/li><li>\u4ee5\u524d\u7528jQuery\uff0c\u53ef\u4ee5\u81ea\u884c\u63a7\u5236DOM\u64cd\u4f5c\u65f6\u673a\uff0c\u624b\u52a8\u8c03\u6574<\/li><li>vue\u548creact\u90fd\u662f\u6570\u636e\u9a71\u52a8\u8bd5\u56fe\uff0c\u5982\u4f55\u6709\u6548\u63a7\u5236DOM\u64cd\u4f5c\uff1f<\/li><\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\u2014\u2014vdom<\/strong><\/p>\n\n\n\n<ul><li>\u6709\u4e00\u5b9a\u7684\u590d\u6742\u5ea6\uff0c\u60f3\u51cf\u5c11\u8ba1\u7b97\u6b21\u6570\u6bd4\u8f83\u96be<\/li><li>\u96be\u4e0d\u80fd\u628a\u8ba1\u7b97\uff0c\u66f4\u591a\u7684\u8f6c\u79fb\u4e3aJS\u8ba1\u7b97\uff1f\u56e0\u4e3aJS\u6267\u884c\u6bd4\u8f83\u5feb<\/li><li><strong>vdom\u2014\u2014\u7528JS\u6a21\u62dfDOM\u7ed3\u6784\uff0c\u8ba1\u7b97\u51fa\u6700\u5c0f\u7684\u53d8\u66f4\uff0c\u64cd\u4f5cDOM<\/strong><\/li><\/ul>\n\n\n\n<p><strong>\u9762\u8bd5\u9898\uff1a\u7528JS\u6a21\u62dfDOM\u5143\u7d20<\/strong>\u00a0<\/p>\n\n\n\n<p>\u5305\u542b\u4e09\u90e8\u5206\uff1a\u6807\u7b7etag\uff0c\u9644\u7740\u5728\u6807\u7b7e\u4e0a\u7684\u5c5e\u6027\u3001\u6837\u5f0f\u3001\u4e8b\u4ef6props\uff0c\u5b50\u5143\u7d20children\u00a0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-1024x551.png\" alt=\"\" class=\"wp-image-6565\" width=\"523\" height=\"280\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-1024x551.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-300x162.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-230x124.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-350x188.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-216-480x258.png 480w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure><\/div>\n\n\n\n<p><strong>vdom\u603b\u7ed3<\/strong><\/p>\n\n\n\n<ul><li>\u7528js\u6a21\u62dfDOM\u7ed3\u6784\uff08vnode\uff09<\/li><li>\u65b0\u65e7vnode\u5bf9\u6bd4\uff0c\u5f97\u51fa\u6700\u5c0f\u7684\u66f4\u65b0\u8303\u56f4\uff0c\u6700\u540e\u66f4\u65b0DOM<\/li><li>\u6570\u636e\u9a71\u52a8\u89c6\u56fe\u7684\u6a21\u5f0f\u4e0b\uff0c\u6709\u6548\u63a7\u5236DOM\u64cd\u4f5c<\/li><\/ul>\n\n\n\n<p><strong>diff\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>\u4e24\u4e2a\u6570\u505adiff\uff0c\u5982\u8fd9\u91cc\u7684vdom diff<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-1024x396.png\" alt=\"\" class=\"wp-image-6567\" width=\"488\" height=\"188\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-1024x396.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-300x116.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-768x297.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-830x321.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-230x89.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-350x135.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217-480x185.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-217.png 1092w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure><\/div>\n\n\n\n<p>vnode \u00a0 ->patch ->new vnode<\/p>\n\n\n\n<p>\u6811diff\u7684\u65f6\u95f4\u590d\u6742\u5ea6O\uff08n^3\uff09<\/p>\n\n\n\n<p>\u7b2c\u4e00\uff0c\u904d\u5386tree1\uff1b\u7b2c\u4e8c\uff0c\u904d\u5386tree2<br>\u7b2c\u4e09\uff0c\u6392\u5e8f<br>1000\u4e2a\u8282\u70b9\uff0c\u8981\u8ba1\u7b971\u4ebf\u6b21\uff0c\u7b97\u6cd5\u4e0d\u53ef\u7528<br>\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u5230O\uff08n\uff09<\/p>\n\n\n\n<p>\u53ea\u6bd4\u8f83\u540c\u4e00\u5c42\u7ea7\uff0c\u4e0d\u8de8\u7ea7\u6bd4\u8f83<br>tag\u4e0d\u76f8\u540c\uff0c\u76f4\u63a5\u5220\u6389\u91cd\u5efa\uff0c\u4e0d\u518d\u6df1\u5ea6\u6bd4\u8f83<br>tag\u548ckey\uff0c\u4e24\u8005\u90fd\u76f8\u540c\uff0c\u5219\u8ba4\u4e3a\u662f\u76f8\u540c\u7684\u8282\u70b9\uff0c\u4e0d\u518d\u6df1\u5ea6\u6bd4\u8f83<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218.png\" alt=\"\" class=\"wp-image-6568\" width=\"502\" height=\"243\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218.png 936w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-300x146.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-768x373.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-830x403.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-230x112.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-350x170.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-218-480x233.png 480w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219.png\" alt=\"\" class=\"wp-image-6569\" width=\"496\" height=\"203\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219.png 944w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-300x123.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-768x314.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-830x339.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-230x94.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-350x143.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-219-480x196.png 480w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure><\/div>\n\n\n\n<p>diff\u7b97\u6cd5\u603b\u7ed3<\/p>\n\n\n\n<ul><li>patchVnode<\/li><li>addVnodes removeVnodes<\/li><li>updateChildren\uff08key\u7684\u91cd\u8981\u6027\uff09<\/li><\/ul>\n\n\n\n<p>vdom\u548cdiff\u603b\u7ed3<br><\/p>\n\n\n\n<ul><li>\u7ec6\u8282\u4e0d\u91cd\u8981\uff0cupdateChildren\u66f4\u65b0\u8fc7\u7a0b\u4e5f\u4e0d\u91cd\u8981\uff0c\u4e0d\u8981\u6df1\u7a76<\/li><li>vnode\u6838\u5fc3\u6982\u5ff5\u5f88\u91cd\u8981\uff1ah vnode patch diff key \u7b49<\/li><li>vnode\u7684\u5b58\u5728\u4ef7\u503c\u66f4\u91cd\u8981\uff1a\u6570\u636e\u9a71\u52a8\u8bd5\u56fe\uff0c\u63a7\u5236DOM\u64cd\u4f5c<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/gxll499294075\/article\/details\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6541"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6541"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6541\/revisions"}],"predecessor-version":[{"id":6570,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6541\/revisions\/6570"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}