国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

詳解vue的hash跳轉原理

瀏覽:3日期:2022-10-02 17:06:39

在new vueRouter的時候我們可以傳入一個mode屬性,他可以接收三個值:hash/history/abstract

詳解vue的hash跳轉原理

hash和history的區別

history的路徑更美觀一點 比如http://yoursite.com/user/id,history是基于pushState()來完成 URL 跳轉而無須重新加載頁面。 但是強制刷新還是會有問題(服務端來解決這個問題),所以history模式需要后端人員配合使用。

hash的路徑會帶有#,比如http://yoursite.com#/user/id

HashHistory

class VueRouter{ constructor(options){ this.matcher = createMatcher(options.routes || []);//這里為了講解hash模式 所以就不進行判斷用戶傳進來的是哪種模式了 this.history = new HashHistory(this);//this vue-router的實例 }}

源碼這里創建了一個基類我們這里和源碼統一,這個基類封裝了三種模式公用的方法和屬性,那么我們在這里創建一個HashHistory和基類History

import History from ’./base’// hash路由export default class HashHistory extends History{ constructor(router){ super(router); //繼承調用父類 等于call }}// 路由的基類export default class History { constructor(router){ this.router = router; }}

如果是hash路由,打開網站如果沒有hash默認應該添加#/

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函數)

init(app){ const history = this.history; // 初始化時,應該先拿到當前路徑,進行匹配邏輯 // 讓路由系統過度到某個路徑 const setupHashListener = ()=> { history.setupListener(); // 監聽路徑變化 } history.transitionTo( // 父類提供方法負責跳轉 history.getCurrentLocation(), // 子類獲取對應的路徑 // 跳轉成功后注冊路徑監聽,為視圖更新做準備 setupHashListener )}

這里我們要分別實現 transitionTo(基類方法)、 getCurrentLocation 、setupListener

getCurrentLocation實現

function getHash(){ return window.location.hash.slice(1);}export default class HashHistory extends History{ // ... getCurrentLocation(){ return getHash(); }}setupListener實現

export default class HashHistory extends History{ // ... setupListener(){ window.addEventListener(’hashchange’, ()=> { // 根據當前hash值 過度到對應路徑 this.transitionTo(getHash()); }) }}TransitionTo實現

export function createRoute(record, location) { // {path:’/’,matched:[record,record]} let res = []; if (record) { // 如果有記錄 while(record){ res.unshift(record); // 就將當前記錄的父親放到前面 record = record.parent } } return { ...location, matched: res }}export default class History { constructor(router) { this.router = router; // 根據記錄和路徑返回對象,稍后會用于router-view的匹配 this.current = createRoute(null, { path: ’/’ }) } // 核心邏輯 transitionTo(location, onComplete) { // 去匹配路徑 let route = this.router.match(location); // 相同路徑不必過渡 if( location === route.path && route.matched.length === this.current.matched.length){ return } //更新路由并且下面會提到改變根實例上的_route屬性 this.updateRoute(route) onComplete && onComplete(); }}

export default class VueRouter{ // ... //做一個代理 match(location){ return this.matcher.match(location); }}macth方法

function match(location){ // 稍后根據路徑找到對應的記錄 let record = pathMap[location] if (record) { // 根據記錄創建對應的路由 //參數:/about/a:{path:xx,component...},path:’/about/a’ return createRoute(record,{ path:location }) } // 找不到則返回空匹配 return createRoute(null, { path: location })}

我們不難發現路徑變化時都會更改current屬性,我們可以把current屬性變成響應式的,每次current變化刷新視圖即可在install方法中

install(Vue) { Vue.mixin({ // 給所有組件的生命周期都增加beforeCreate方法 beforeCreate() { if (this.$options.router) { //調用Vue類中雙向數據綁定方法 Vue.util.defineReactive(this,’_route’,this._router.history.current); } } }); // $route和$router方法 這兩個方法僅僅是vue中最常見的代理 僅僅是為了更加方便 Object.defineProperty(Vue.prototype,’$route’,{ // 每個實例都可以獲取到$route屬性 get(){ return this._routerRoot._route;//上面剛進行雙向數據綁定的 } }); Object.defineProperty(Vue.prototype,’$router’,{ // 每個實例都可以獲取router實例 get(){ return this._routerRoot._router; } }) }

切換路由每次初始化時都需要調用更新_route的方法,因為install的時候把_route進行雙向數據綁定,剛進來是沒有this._router.history.current的,通過發布訂閱方式來進行訂閱和更新操作;在init方法中增加監聽函數

history.listen((route) => { // 需要更新_route屬性,出入一個函數 app._route = route});

export default class History { constructor(router) { // ... this.cb = null; } listen(cb){ this.cb = cb; // 注冊函數 } updateRoute(route){ this.current =route; this.cb && this.cb(route); // 更新current后 更新_route屬性 }}

以上就是詳解vue的hash跳轉原理的詳細內容,更多關于vue的hash跳轉原理的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 亚洲毛片在线免费观看 | 在线免费观看日本视频 | 成人午夜视频免费观看 | 国产精品大片天天看片 | 一级做a爱 一区 | 免费一极毛片 | 9191久久久久视频 | 美国一级片在线 | 国产手机视频 | 欧美成人免费在线 | 久久久久久极精品久久久 | 国产在线观看午夜不卡 | 成人午夜视频免费观看 | 中文字幕天堂久久精品 | 亚洲国产欧美自拍 | 精品一区二区三区五区六区 | 成人国产片免费 | 成在线人免费视频 | 91视频社区 | 国产三级精品最新在线 | 手机看片久久青草福利盒子 | 亚洲国产精品综合久久20 | 久草播放 | 国产一级性生活 | 最新版天堂资源中文官网 | 日韩一级特黄 | 国产成人精品视频一区二区不卡 | 色香欲综合成人免费视频 | 国产精品久久久久激情影院 | 亚洲精品亚洲人成人网 | 亚洲国产激情在线一区 | 一区二区在线播放福利视频 | 成年人看的免费视频 | 日本乱理伦中文三区 | 手机看片1024精品日韩 | 亚洲精品推荐 | 91视频一区二区 | 操出白浆视频 | 成人国产片免费 | 亚洲天堂一区在线 | 足恋玩丝袜脚视频免费网站 |