使用
1
2
3
4
5
|
< div id = "app" > < router-link to = 'home' >首頁</ router-link > < router-link to = 'about' >關于</ router-link > < router-view a = 1 >< router-view /> </ div > |
router-view組件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
export default { //函數式組件沒有this 不能new 沒有雙向數據綁定,通常用的比較少,比較適用于展示詳情頁因為詳情頁只展示不進行修改等操作,函數式組件比有狀態的組件更加輕量級。 functional: true , render(h,{parent,data}){ parent表示的父組件 app data 是行間屬性(上面代碼a=1) 也可以使用prop傳遞 let route = parent.$route; let depth = 0; data.routerView = true ; while (parent){ //$vnode指的是虛擬dom 如果app上有虛擬dom并且這個虛擬dom上的routerView為true if (parent.$vnode && parent.$vnode.data.routerView){ depth++; } parent = parent.$parent; } let record = route.matched[depth]; if (!record){ return h(); } return h(record.component, data); } } |
router-link的實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
export default { props:{ to:{ type:String, required: true }, tag:{ type:String } }, render(h){ let tag = this .tag || 'a' ; let handler = ()=>{ this .$router.push( this .to); } return <tag onClick={handler}>{ this .$slots. default }</tag> } } |
到此這篇關于Vue router-view和router-link的實現原理的文章就介紹到這了,更多相關Vue router-view和router-link內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://segmentfault.com/a/1190000039369356