詳解vue的hash跳轉(zhuǎn)原理
在new vueRouter的時(shí)候我們可以傳入一個(gè)mode屬性,他可以接收三個(gè)值:hash/history/abstract
hash和history的區(qū)別
history的路徑更美觀一點(diǎn) 比如http://yoursite.com/user/id,history是基于pushState()來(lái)完成 URL 跳轉(zhuǎn)而無(wú)須重新加載頁(yè)面。 但是強(qiáng)制刷新還是會(huì)有問(wèn)題(服務(wù)端來(lái)解決這個(gè)問(wèn)題),所以history模式需要后端人員配合使用。
hash的路徑會(huì)帶有#,比如http://yoursite.com#/user/id
HashHistory
class VueRouter{ constructor(options){ this.matcher = createMatcher(options.routes || []); //這里為了講解hash模式 所以就不進(jìn)行判斷用戶傳進(jìn)來(lái)的是哪種模式了 this.history = new HashHistory(this);//this vue-router的實(shí)例 } }
源碼這里創(chuàng)建了一個(gè)基類我們這里和源碼統(tǒng)一,這個(gè)基類封裝了三種模式公用的方法和屬性,那么我們?cè)谶@里創(chuàng)建一個(gè)HashHistory和基類History
import History from './base' // hash路由 export default class HashHistory extends History{ constructor(router){ super(router); //繼承調(diào)用父類 等于call } } // 路由的基類 export default class History { constructor(router){ this.router = router; } }
如果是hash路由,打開(kāi)網(wǎng)站如果沒(méi)有hash默認(rèn)應(yīng)該添加#/
import History from './base'; function ensureSlash(){ if(window.location.hash){ return } window.location.hash = '/' } export default class HashHistory extends History{ constructor(router){ super(router); ensureSlash(); // 確保有hash } }
再看一下初始化的邏輯(上面的router.init函數(shù))
init(app){ const history = this.history; // 初始化時(shí),應(yīng)該先拿到當(dāng)前路徑,進(jìn)行匹配邏輯 // 讓路由系統(tǒng)過(guò)度到某個(gè)路徑 const setupHashListener = ()=> { history.setupListener(); // 監(jiān)聽(tīng)路徑變化 } history.transitionTo( // 父類提供方法負(fù)責(zé)跳轉(zhuǎn) history.getCurrentLocation(), // 子類獲取對(duì)應(yīng)的路徑 // 跳轉(zhuǎn)成功后注冊(cè)路徑監(jiān)聽(tīng),為視圖更新做準(zhǔn)備 setupHashListener ) }
這里我們要分別實(shí)現(xiàn) transitionTo(基類方法)、 getCurrentLocation 、setupListener
getCurrentLocation實(shí)現(xiàn)
function getHash(){ return window.location.hash.slice(1); } export default class HashHistory extends History{ // ... getCurrentLocation(){ return getHash(); } }
setupListener實(shí)現(xiàn)
export default class HashHistory extends History{ // ... setupListener(){ window.addEventListener('hashchange', ()=> { // 根據(jù)當(dāng)前hash值 過(guò)度到對(duì)應(yīng)路徑 this.transitionTo(getHash()); }) } }
TransitionTo實(shí)現(xiàn)
export function createRoute(record, location) { // {path:'/',matched:[record,record]} let res = []; if (record) { // 如果有記錄 while(record){ res.unshift(record); // 就將當(dāng)前記錄的父親放到前面 record = record.parent } } return { ...location, matched: res } } export default class History { constructor(router) { this.router = router; // 根據(jù)記錄和路徑返回對(duì)象,稍后會(huì)用于router-view的匹配 this.current = createRoute(null, { path: '/' }) } // 核心邏輯 transitionTo(location, onComplete) { // 去匹配路徑 let route = this.router.match(location); // 相同路徑不必過(guò)渡 if( location === route.path && route.matched.length === this.current.matched.length){ return } //更新路由并且下面會(huì)提到改變根實(shí)例上的_route屬性 this.updateRoute(route) onComplete && onComplete(); } }
export default class VueRouter{ // ... //做一個(gè)代理 match(location){ return this.matcher.match(location); } }
macth方法
function match(location){ // 稍后根據(jù)路徑找到對(duì)應(yīng)的記錄 let record = pathMap[location] if (record) { // 根據(jù)記錄創(chuàng)建對(duì)應(yīng)的路由 //參數(shù):/about/a:{path:xx,component...},path:'/about/a' return createRoute(record,{ path:location }) } // 找不到則返回空匹配 return createRoute(null, { path: location }) }
我們不難發(fā)現(xiàn)路徑變化時(shí)都會(huì)更改current屬性,我們可以把current屬性變成響應(yīng)式的,每次current變化刷新視圖即可
在install方法中
install(Vue) { Vue.mixin({ // 給所有組件的生命周期都增加beforeCreate方法 beforeCreate() { if (this.$options.router) { //調(diào)用Vue類中雙向數(shù)據(jù)綁定方法 Vue.util.defineReactive(this,'_route',this._router.history.current); } } }); // $route和$router方法 這兩個(gè)方法僅僅是vue中最常見(jiàn)的代理 僅僅是為了更加方便 Object.defineProperty(Vue.prototype,'$route',{ // 每個(gè)實(shí)例都可以獲取到$route屬性 get(){ return this._routerRoot._route;//上面剛進(jìn)行雙向數(shù)據(jù)綁定的 } }); Object.defineProperty(Vue.prototype,'$router',{ // 每個(gè)實(shí)例都可以獲取router實(shí)例 get(){ return this._routerRoot._router; } }) }
切換路由每次初始化時(shí)都需要調(diào)用更新_route的方法,因?yàn)閕nstall的時(shí)候把_route進(jìn)行雙向數(shù)據(jù)綁定,剛進(jìn)來(lái)是沒(méi)有this._router.history.current的,通過(guò)發(fā)布訂閱方式來(lái)進(jìn)行訂閱和更新操作;在init方法中增加監(jiān)聽(tīng)函數(shù)
history.listen((route) => { // 需要更新_route屬性,出入一個(gè)函數(shù) app._route = route });
export default class History { constructor(router) { // ... this.cb = null; } listen(cb){ this.cb = cb; // 注冊(cè)函數(shù) } updateRoute(route){ this.current =route; this.cb && this.cb(route); // 更新current后 更新_route屬性 } }
以上就是詳解vue的hash跳轉(zhuǎn)原理的詳細(xì)內(nèi)容,更多關(guān)于vue的hash跳轉(zhuǎn)原理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Vant UI框架實(shí)現(xiàn)時(shí)間段選擇器
這篇文章主要為大家詳細(xì)介紹了基于Vant UI框架實(shí)現(xiàn)時(shí)間段選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12vue如何解決數(shù)據(jù)加載時(shí),插值表達(dá)式閃爍問(wèn)題
這篇文章主要介紹了vue如何解決數(shù)據(jù)加載時(shí),插值表達(dá)式閃爍問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Vue自定義指令實(shí)現(xiàn)點(diǎn)擊右鍵彈出菜單示例詳解
這篇文章主要為大家介紹了Vue自定義指令實(shí)現(xiàn)點(diǎn)擊右鍵彈出菜單示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01vue父組件觸發(fā)事件改變子組件的值的方法實(shí)例詳解
這篇文章主要介紹了vue父組件觸發(fā)事件改變子組件的值的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Vue響應(yīng)式原理Observer、Dep、Watcher理解
這篇文章主要介紹了Vue響應(yīng)式原理-理解Observer、Dep、Watcher,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06通過(guò)fastclick源碼分析徹底解決tap“點(diǎn)透”
這篇文章主要介紹了通過(guò)fastclick源碼分析徹底解決tap“點(diǎn)透”問(wèn)題的知識(shí)內(nèi)容,有興趣的朋友學(xué)習(xí)一下吧。2017-12-12