Vue router-view和router-link的實現(xiàn)原理
使用
<div id='app'> <router-link to=’home’>首頁</router-link> <router-link to=’about’>關于</router-link> <router-view a=1><router-view/> </div>
router-view組件
export default {//函數(shù)式組件沒有this 不能new 沒有雙向數(shù)據(jù)綁定,通常用的比較少,比較適用于展示詳情頁因為詳情頁只展示不進行修改等操作,函數(shù)式組件比有狀態(tài)的組件更加輕量級。 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的實現(xiàn)
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的實現(xiàn)原理的文章就介紹到這了,更多相關Vue router-view和router-link內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)2. JAMon(Java Application Monitor)備忘記3. Java8內(nèi)存模型PermGen Metaspace實例解析4. 學python最電腦配置有要求么5. python中用Scrapy實現(xiàn)定時爬蟲的實例講解6. 基于python實現(xiàn)操作git過程代碼解析7. python使用QQ郵箱實現(xiàn)自動發(fā)送郵件8. 增大python字體的方法步驟9. Python 的 __str__ 和 __repr__ 方法對比10. Python TestSuite生成測試報告過程解析
