vue從使用到源碼實(shí)現(xiàn)教程詳解
搭建環(huán)境
項(xiàng)目中涉及了json-server模擬get請(qǐng)求,用了vue-router;
關(guān)于Vue生命周期以及vue-router鉤子函數(shù)詳解
生命周期
1.0版本
1.哪些生命周期接口
init Created beforeCompile Compiled Ready Attatched Detached beforeDestory destoryed
2.執(zhí)行順序
1. 不具有keep-alive
進(jìn)入:
init->create->beforeCompile->complied->attatched->ready
移出:
beforeDestory->detached->destoryed;
2. 具有keep-alive
第一次的時(shí)候
進(jìn)入:
init->create->beforeCompile->complied->attatched->ready
移出:
detached;
之后的每次
進(jìn)入:
attatched
移出:
detached
鉤子函數(shù)
3.鉤子函數(shù)有哪些
data
activete
deactivate
canactivate
candeactivate
4.執(zhí)行順序
進(jìn)入:
canactivate->actiavte->date
移出:
candeactivate->deactiavte
兩者一起出現(xiàn)
5.對(duì)于一個(gè)組件A里面有子組件B,當(dāng)這個(gè)組件A進(jìn)行移入和移出操作時(shí),組件之間的生命周期喝鉤子函數(shù)的執(zhí)行順序參考如下:
例如
A.vue
<div>
<B></B>
</div>
備注:下面括號(hào)里的是嵌套的子組件
1. 不具有keep-alive:
移入:
1. canActivate;
2. init;
3. create;
4. beforeCompile;
5. (嵌套子組件:init,create,beforeCompile,compile);
6. compile;
7. activate;
8. data;
9. attached;
10. (子組件attached);
11. (子組件ready);
12. ready;
移出:
13. canDeactivate;
14. deactivate;
15. beforeDestroy;
16. (子組件beforeDestroy);
17. (子組件destoryed);
18. detached;
19. (子組件detached);
20. destoryed;
2. 具有keep-alive:
移入:
1. canActivate;
2. activate;
3. data;
4. attached;
5. (子組件attached);
移出:
6. canDeactivate;
7. deactivate;
8. detached;
9. (子組件detached);
6.鉤子函數(shù)activate和data的執(zhí)行順序
涉及鉤子函數(shù)異步 resolve 規(guī)則:
1.如果鉤子返回一個(gè) Promise,則鉤子何時(shí) resolve 取決于該 Promise 何時(shí) resolve。
2.如果鉤子既不返回 Promise,也沒(méi)有任何參數(shù),則該鉤子將被同步 resolve。
3.如果鉤子不返回 Promise,但是有一個(gè)參數(shù)(transition),則鉤子會(huì)等到transition.next(),transition.abort()或是transition.redirect()之一被調(diào)用才 resolve。
4.在驗(yàn)證類的鉤子,比如canActivate,canDeactivate以及全局 beforeEach 鉤子中,如果返回值是一個(gè)布爾值 (Boolean),也會(huì)使得鉤子同步 resolve。

7.根據(jù)什么可以確保界面已經(jīng)更新完成,也就是說(shuō)掛在完成
執(zhí)行生命周期attached說(shuō)明已掛載
雙向綁定與渲染機(jī)制
1.數(shù)據(jù)的監(jiān)聽(tīng)和觸發(fā)(訂閱和發(fā)布o(jì)bserver)
src目錄下observer:
1. array.js
2. dep.js;(實(shí)現(xiàn)一個(gè)發(fā)布訂閱對(duì)象)
3. index.js;(利用Object.defineProperty這個(gè)API,并為此屬性設(shè)計(jì)一個(gè)特殊的 getter/setter,然后在 setter 里觸發(fā)一個(gè)函數(shù),達(dá)到監(jiān)聽(tīng)的效果);
下面是這部分的源碼
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter () {
var value = getter ? getter.call(obj) : val
if (Dep.target) {
dep.depend()
if (childOb) {
childOb.dep.depend()
}
if (isArray(value)) {
for (var e, i = 0, l = value.length; i < l; i++) {
e = value[i]
e && e.__ob__ && e.__ob__.dep.depend()
}
}
}
return value
},
set: function reactiveSetter (newVal) {
var value = getter ? getter.call(obj) : val
if (newVal === value) {
return
}
if (setter) {
setter.call(obj, newVal)
} else {
val = newVal
}
childOb = observe(newVal)
dep.notify()
}
})
簡(jiǎn)化上面的監(jiān)聽(tīng)與觸發(fā)代碼如下:
function notidy(obj,key){
console.log(key+" has changed");
console.log(key+" now is: "+obj[key]);
}
function ToData(key,val){
var ob=this;
Object.defineProperty(ob,key,{
enumerable:true,
configurable:true,
get:function(){
return val;
},
set:function(newval){
if(newval==val){
return;
}
val=newval;
notidy(this,key);
}
})
}
src目錄下directive.js
在directive中可以看到一系列解析出來(lái)的屬性,而directive的實(shí)例化可以在utils/lifecycle.js中看到。
下面這段代碼在Directive.prototype._bind中
var watcher = this._watcher = new Watcher(
this.vm,
this.expression,
this._update, // callback
{
filters: this.filters,
twoWay: this.twoWay,
deep: this.deep,
preProcess: preProcess,
postProcess: postProcess,
scope: this._scope
}
)
// v-model with inital inline value need to sync back to
// model instead of update to DOM on init. They would
// set the afterBind hook to indicate that.
if (this.afterBind) {
this.afterBind()
} else if (this.update) {
this.update(watcher.value)
}
Directive.prototype.set = function (value) {
/* istanbul ignore else */
if (this.twoWay) {
this._withLock(function () {
this._watcher.set(value)
})
} else if (process.env.NODE_ENV !== 'production') {
warn(
'Directive.set() can only be used inside twoWay' +
'directives.'
)
}
}
src目錄下Watch.js:
從下面的代碼可以找到watcher對(duì)象通過(guò)addDep方法實(shí)現(xiàn)訂閱
Watcher.prototype.addDep = function (dep) {
var id = dep.id
if (!this.newDepIds.has(id)) {
this.newDepIds.add(id)
this.newDeps.push(dep)
if (!this.depIds.has(id)) {
dep.addSub(this)
}
}
}
2.前面說(shuō)那么多關(guān)于雙向綁定,其實(shí)這也是VUE內(nèi)部的渲染機(jī)制,總結(jié)如下
1. 通過(guò) observer 對(duì) data 進(jìn)行了監(jiān)聽(tīng),并且提供訂閱某個(gè)數(shù)據(jù)項(xiàng)的變化的能力
2. 把 template 解析成一段 document fragment,然后解析其中的 directive,得到每一個(gè) directive 所依賴的數(shù)據(jù)項(xiàng)及其更新方法。比如 v-text="message" 被解析之后 (這里僅作示意,實(shí)際程序邏輯會(huì)更嚴(yán)謹(jǐn)而復(fù)雜):所依賴的數(shù)據(jù)項(xiàng)this.$data.message,以及相應(yīng)的視圖更新方法 node.textContent = this.$data.message
3. 通過(guò) watcher 把上述兩部分結(jié)合起來(lái),即把 directive 中的數(shù)據(jù)依賴訂閱在對(duì)應(yīng)數(shù)據(jù)的 observer 上,這樣當(dāng)數(shù)據(jù)變化的時(shí)候,就會(huì)觸發(fā) observer,進(jìn)而觸發(fā)相關(guān)依賴對(duì)應(yīng)的視圖更新方法,最后達(dá)到模板原本的關(guān)聯(lián)效果。
3.vue是如何改進(jìn)了v-for具有相同數(shù)據(jù)渲染出錯(cuò)的?
數(shù)組的渲染
未使用track-by的數(shù)組渲染內(nèi)部緩存的默認(rèn)id是數(shù)組的值value,意味著如果數(shù)組中存在相同的值,通過(guò)id獲取的是相同的一個(gè)fragement片段,最后通過(guò)insertBefore操作DOM由于是相同的一個(gè)實(shí)例,故不會(huì)生效。
<div>
<ul id='test'>
<li id="child1">child1</li>
<li id="child">child2</li>
</ul>
</div>
<script>
_element1=document.getElementById('child1');
_element2=document.getElementById('child2');
document.getElementById('test').insertBefore(_element1,_element2);
</script>
渲染的結(jié)果是child2在child1前面
使用track-by目的是自定義這個(gè)內(nèi)部的id,使得數(shù)組中具有相同的值的幾項(xiàng)都不會(huì)選擇到相同的實(shí)例,對(duì)于使用track-by='$index'還是其他唯一區(qū)分的id值有一定的區(qū)別,各有好處。
使用$index使得反轉(zhuǎn)的數(shù)據(jù)沒(méi)有移動(dòng)操作,而對(duì)于使用其他的id在順序不一樣的時(shí)候會(huì)有相應(yīng)的移動(dòng)操作。
對(duì)象的渲染
對(duì)象一般使用鍵作為內(nèi)部緩存對(duì)象的id,通過(guò)track-by也可以自定義這個(gè)id提高性能。
vm.model = {
a: { id: 1, val: "model1"},
b: { id: 2, val: "model2"},
c: { id: 3, val: "model2"},
}
列表更新
vm.model = {
d: { id: 1, val: "model1"},
e: { id: 2, val: "model2"},
f: { id: 3, val: "model2"}
}
以上所述是小編給大家介紹的vue從使用到源碼實(shí)現(xiàn)教程詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue element-ui el-cascader級(jí)聯(lián)選擇器數(shù)據(jù)回顯的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了vue element-ui el-cascader級(jí)聯(lián)選擇器數(shù)據(jù)回顯的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-07-07
使用Vue3新特性構(gòu)建動(dòng)態(tài)表單的方法詳解
傳統(tǒng)的表單開(kāi)發(fā)通常需要編寫大量的重復(fù)代碼,例如處理用戶輸入、驗(yàn)證數(shù)據(jù)、更新 UI 等等,為了簡(jiǎn)化開(kāi)發(fā),我們可以借助 Vue 3 的新特性,例如組合式 API 和 ref 對(duì)象,所以本文我們將一起學(xué)習(xí)如何使用 Vue 3 的新特性構(gòu)建一個(gè)更加靈活、可擴(kuò)展的動(dòng)態(tài)表單2024-06-06
vue router嵌套路由在history模式下刷新無(wú)法渲染頁(yè)面問(wèn)題的解決方法
這篇文章主要介紹了vue router嵌套路由在history模式下刷新無(wú)法渲染頁(yè)面問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Vue項(xiàng)目中首頁(yè)長(zhǎng)時(shí)間白屏的原因以及解決過(guò)程
這篇文章主要介紹了Vue項(xiàng)目中首頁(yè)長(zhǎng)時(shí)間白屏的原因以及解決過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Vue2中compiler和runtime模式報(bào)錯(cuò)template compiler is 
本文主要介紹了Vue2中compiler和runtime模式報(bào)錯(cuò)template compiler is not available,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Antd中單個(gè)DatePicker限定時(shí)間輸入范圍操作
這篇文章主要介紹了Antd中單個(gè)DatePicker限定時(shí)間輸入范圍操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue使用window.open()跳轉(zhuǎn)頁(yè)面的代碼案例
這篇文章主要介紹了vue中對(duì)window.openner的使用,vue使用window.open()跳轉(zhuǎn)頁(yè)面的代碼案例,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11

