vue面試created中兩次數(shù)據(jù)修改會觸發(fā)幾次頁面更新詳解
面試題:
created
生命周期中兩次修改數(shù)據(jù),會觸發(fā)幾次頁面更新?
一、同步的
先舉個簡單的同步的例子:
new Vue({ el: "#app", template: `<div> <div>{{count}}</div> </div>`, data() { return { count: 1, } }, created() { this.count = 2; this.count = 3; }, });
在created
生命周期中,通過this.count = 2
和this.count = 3
的方式將this.count
重新賦值。
這里直接拋出答案:渲染一次。
為什么?
這個與數(shù)據(jù)的響應(yīng)式處理有關(guān),先看響應(yīng)式處理的邏輯:
export function defineReactive ( obj: Object, key: string, val: any, customSetter?: ?Function, shallow?: boolean ) { // 重點:創(chuàng)建一個發(fā)布者實例 const dep = new Dep() const property = Object.getOwnPropertyDescriptor(obj, key) if (property && property.configurable === false) { return } // cater for pre-defined getter/setters const getter = property && property.get const setter = property && property.set if ((!getter || setter) && arguments.length === 2) { val = obj[key] } let childOb = !shallow && observe(val) Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter () { const value = getter ? getter.call(obj) : val if (Dep.target) { // 重點:進行當前正在計算的渲染W(wǎng)atcher的收集 dep.depend() if (childOb) { childOb.dep.depend() if (Array.isArray(value)) { dependArray(value) } } } return value }, set: function reactiveSetter (newVal) { const value = getter ? getter.call(obj) : val /* eslint-disable no-self-compare */ if (newVal === value || (newVal !== newVal && value !== value)) { return } /* eslint-enable no-self-compare */ if (process.env.NODE_ENV !== 'production' && customSetter) { customSetter() } // #7981: for accessor properties without setter if (getter && !setter) return if (setter) { setter.call(obj, newVal) } else { val = newVal } childOb = !shallow && observe(newVal) // 重點:當數(shù)據(jù)發(fā)生變化時,發(fā)布者實例dep會通知收集到的watcher進行更新 dep.notify() } }) }
在數(shù)據(jù)響應(yīng)式處理階段,會實例化一個發(fā)布者dep
,并且通過Object.defineProperty
的方式為當前數(shù)據(jù)定義get
和set
函數(shù)。在生成虛擬vNode
的階段,會觸發(fā)get
函數(shù)中會進行當前正在計算的渲染Watcher
的收集,此時,發(fā)布者dep
的subs
中會多一個渲染Watcher
實例。在數(shù)據(jù)發(fā)生變化的時候,會觸發(fā)set
函數(shù),通知發(fā)布者dep
中subs
中的watcher
進行更新。
至于數(shù)據(jù)修改會觸發(fā)幾次更新,就與當前發(fā)布者dep
的subs
中收集了幾次渲染watcher
有關(guān)了,再看watcher
收集和created
執(zhí)行之間的順序:
Vue.prototype._init = function (options) { // ... initState(vm); // ... callHook(vm, 'created'); // ... if (vm.$options.el) { vm.$mount(vm.$options.el); } }
我們知道在initState(vm)
階段對數(shù)據(jù)進行響應(yīng)式處理,但是此時發(fā)布者dep
的subs
還是空數(shù)組。當執(zhí)行callHook(vm, 'created')
的時候,會執(zhí)行this.count = 2
和this.count = 3
的邏輯,也的確會觸發(fā)set
函數(shù)中的dep.notify
通知收集到的watcher
進行更新。但是,此時dep
的subs
是空數(shù)組,相當于啥也沒做。
只有在vm.$mount(vm.$options.el)
執(zhí)行過程中,生成虛擬vNode
的時候才會進行渲染Watcher
收集,此時,dep
的subs
才不為空。最終,通過vm.$mount(vm.$options.el)
進行了頁面的一次渲染,并未因為this.count=2
或者this.count=3
而觸發(fā)多余的頁面更新。
簡言之,就是created
鉤子函數(shù)內(nèi)的邏輯的執(zhí)行是在渲染watcher
收集之前執(zhí)行的,所以未引起因為數(shù)據(jù)變化而導(dǎo)致的頁面更新。
二、異步的
同步的場景說完了,我們再舉個異步的例子:
new Vue({ el: "#app", template: `<div> <div>{{count}}</div> </div>`, data() { return { count: 1, } }, created() { setTimeout(() => { this.count = 2; }, 0) setTimeout(() => { this.count = 3; }, 0) }, });
在created
生命周期中,通過異步的方式執(zhí)行this.count = 2
和this.count = 3
的方式將this.count
重新賦值。
這里直接拋出答案:首次渲染一次,因為數(shù)據(jù)變化導(dǎo)致的頁面更新兩次。
為什么?
這個就與eventLoop
事件循環(huán)機制有關(guān)了,我們知道javascript
是一個單線程執(zhí)行的語言,當我們通過new Vue
實例化的過程中,會執(zhí)行初始化方法this._init
方法,開始了Vue
底層的處理邏輯。當遇到setTimeout
異步操作時,會將其推入到異步隊列中去,等待當前同步任務(wù)執(zhí)行完以后再去異步隊列中取出隊首元素進行執(zhí)行。
當前例子中,在initState(vm)
階段對數(shù)據(jù)進行響應(yīng)式處理。當執(zhí)行callHook(vm, 'created')
的時候,會將this.count = 2
和this.count = 3
的邏輯推入到異步隊列等待執(zhí)行。繼續(xù)執(zhí)行vm.$mount(vm.$options.el)
的過程中會去生成虛擬vNode
,進而觸發(fā)get
函數(shù)的渲染Watcher
收集,此時,dep
的subs
中就有了一個渲染watcher
。
等首次頁面渲染完成以后,會去執(zhí)行this.count=2
的邏輯,數(shù)據(jù)的修改會觸發(fā)set
函數(shù)中的dep.notify
,此時發(fā)布者dep
的subs
不為空,會引起頁面的更新。同理,this.count=3
會再次引起頁面數(shù)據(jù)的更新。也就是說,首次渲染一次,因為this.count=2
和this.count=3
還會導(dǎo)致頁面更新兩次。
三、附加
如果我改變的值和data
中定義的值一致呢?
new Vue({ el: "#app", template: `<div> <div>{{count}}</div> </div>`, data() { return { count: 1, } }, created() { setTimeout(() => { this.count = 1; }, 0) }, });
這個時候,在觸發(fā)set
的邏輯中,會當執(zhí)行到if (newVal === value || (newVal !== newVal && value !== value)) { return }
的邏輯,不會再執(zhí)行到dep.notify
,這種場景下數(shù)據(jù)的數(shù)據(jù)也不會引起頁面的再次更新。
總結(jié)
從生命周期created
和頁面渲染的先后順序,Object.defineProperty
觸發(fā)get
和set
函數(shù)的機理,以及eventLoop
事件循環(huán)機制入手,去分析created
中兩次數(shù)據(jù)修改會觸發(fā)幾次頁面更新的問題就會清晰很多。
以上就是vue面試created中兩次數(shù)據(jù)修改會觸發(fā)幾次頁面更新詳解的詳細內(nèi)容,更多關(guān)于vue created數(shù)據(jù)修改頁面更新的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue獲取DOM元素并設(shè)置屬性的兩種實現(xiàn)方法
下面小編就為大家?guī)硪黄獀ue獲取DOM元素并設(shè)置屬性的兩種實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09vue mounted()函數(shù)中無法定義初始化樣式的原因分析
這篇文章主要介紹了vue mounted()函數(shù)中無法定義初始化樣式的原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03VUE v-model表單數(shù)據(jù)雙向綁定完整示例
這篇文章主要介紹了VUE v-model表單數(shù)據(jù)雙向綁定,結(jié)合完整實例形式分析了vue.js實現(xiàn)表單數(shù)據(jù)雙向綁定相關(guān)操作技巧,需要的朋友可以參考下2019-01-01vue使用高德地圖實現(xiàn)添加點標記和獲取點擊位置信息的示例代碼
這篇文章主要介紹了vue使用高德地圖實現(xiàn)添加點標記和獲取點擊位置信息的示例代碼,文中補充介紹了高德vue-amap使用(一)標記點位獲取地址及經(jīng)緯度,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2024-01-01vue中created、watch和computed的執(zhí)行順序詳解
由于vue的雙向數(shù)據(jù)綁定,自動更新數(shù)據(jù)的機制,在數(shù)據(jù)變化后,對此數(shù)據(jù)依賴?的所有數(shù)據(jù),watch事件都會被更新、觸發(fā),下面這篇文章主要給大家介紹了關(guān)于vue中created、watch和computed的執(zhí)行順序,需要的朋友可以參考下2022-11-11