日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

如何手寫簡易的 Vue Router

瀏覽:147日期:2022-11-16 15:05:40

前言

還是那樣,懂得如何使用一個常用庫,還得了解其原理或者怎么模擬實現,今天實現一下 vue-router 。

有一些知識我這篇文章提到了,這里就不詳細一步步寫,請看我 手寫一個簡易的 Vuex

基本骨架

Vue 里面使用插件的方式是 Vue.use(plugin) ,這里貼出它的用法:

安裝 Vue.js 插件。如果插件是一個對象,必須提供 install 方法。如果插件是一個函數,它會被作為 install 方法。install 方法調用時,會將 Vue 作為參數傳入。這個方法的第一個參數是 Vue 構造器,第二個參數是一個可選的選項對象。

全局混入

使用 Vue.mixin(mixin)

全局注冊一個混入,影響注冊之后所有創建的每個 Vue 實例。可以使用混入向組件注入自定義的行為,它將影響每一個之后創建的 Vue 實例。

路由用法

比如簡單的:

// 路由數組const routes = [ { path: ’/’, name: ’Page1’, component: Page1, }, { path: ’/page2’, name: ’Page2’, component: Page2, },]const router = new VueRouter({ mode: ’history’, // 模式 routes,})

它是傳入了mode和routes,我們實現的時候需要在VueRouter構造函數中接收。

在使用路由標題的時候是這樣:

<p> <!-- 使用 router-link 組件來導航. --> <!-- 通過傳入 `to` 屬性指定鏈接. --> <!-- <router-link> 默認會被渲染成一個 `<a>` 標簽 --> <router-link to='/page1'>Go to Foo</router-link> <router-link to='/page2'>Go to Bar</router-link></p><!-- 路由出口 --><!-- 路由匹配到的組件將渲染在這里 --><router-view></router-view>

故我們需要使用Vue.component( id, [definition] )注冊一個全局組件。

了解了大概,我們就可以寫出一個基本骨架

let Vue = nullclass VueRouter { constructor(options) { this.mode = options.mode || ’hash’ this.routes = options.routes || [] }}VueRouter.install = function (_Vue) { Vue = _Vue Vue.mixin({ beforeCreate() { // 根組件 if (this.$options && this.$options.router) { this._root = this // 把當前vue實例保存到_root上 this._router = this.$options.router // 把router的實例掛載在_router上 } else if (this.$parent && this.$parent._root) { // 子組件的話就去繼承父組件的實例,讓所有組件共享一個router實例 this._root = this.$parent && this.$parent._root } }, }) Vue.component(’router-link’, { props: { to: { type: [String, Object], required: true, }, tag: { type: String, default: ’a’, // router-link 默認渲染成 a 標簽 }, }, render(h) { let tag = this.tag || ’a’ return <tag href={this.to}>{this.$slots.default}</tag> }, }) Vue.component(’router-view’, { render(h) { return h(’h1’, {}, ’視圖顯示的地方’) // 暫時置為h1標簽,下面會改 }, })}export default VueRouter

mode

vue-router有兩種模式,默認為 hash 模式。

history 模式

通過window.history.pushStateAPI 來添加瀏覽器歷史記錄,然后通過監聽popState事件,也就是監聽歷史記錄的改變,來加載相應的內容。

popstate 事件

當活動歷史記錄條目更改時,將觸發 popstate 事件。如果被激活的歷史記錄條目是通過對 history.pushState()的調用創建的,或者受到對 history.replaceState()的調用的影響,popstate 事件的 state 屬性包含歷史條目的狀態對象的副本。

History.pushState()方法

window.history.pushState(state, title, url)

該方法用于在歷史中添加一條記錄,接收三個參數,依次為:

state:一個與添加的記錄相關聯的狀態對象,主要用于popstate事件。該事件觸發時,該對象會傳入回調函數。也就是說,瀏覽器會將這個對象序列化以后保留在本地,重新載入這個頁面的時候,可以拿到這個對象。如果不需要這個對象,此處可以填null。 title:新頁面的標題。但是,現在所有瀏覽器都忽視這個參數,所以這里可以填空字符串。 url:新的網址,必須與當前頁面處在同一個域。瀏覽器的地址欄將顯示這個網址。

hash 模式

使用 URL 的 hash 來模擬一個完整的 URL。,通過監聽hashchange事件,然后根據hash值(可通過 window.location.hash 屬性讀取)去加載對應的內容的。

繼續增加代碼,

let Vue = nullclass HistoryRoute { constructor() { this.current = null // 當前路徑 }}class VueRouter { constructor(options) { this.mode = options.mode || ’hash’ this.routes = options.routes || [] this.routesMap = this.createMap(this.routes) this.history = new HistoryRoute() // 當前路由 this.initRoute() // 初始化路由函數 } createMap(routes) { return routes.reduce((pre, current) => { pre[current.path] = current.component return pre }, {}) } initRoute() { if (this.mode === ’hash’) { // 先判斷用戶打開時有沒有hash值,沒有的話跳轉到 #/ location.hash ? ’’ : (location.hash = ’/’) window.addEventListener(’load’, () => { this.history.current = location.hash.slice(1) }) window.addEventListener(’hashchange’, () => { this.history.current = location.hash.slice(1) }) } else { // history模式 location.pathname ? ’’ : (location.pathname = ’/’) window.addEventListener(’load’, () => { this.history.current = location.pathname }) window.addEventListener(’popstate’, () => { this.history.current = location.pathname }) } }}VueRouter.install = function (_Vue) { Vue = _Vue Vue.mixin({ beforeCreate() { if (this.$options && this.$options.router) { this._root = this this._router = this.$options.router Vue.util.defineReactive(this, ’_route’, this._router.history) // 監聽history路徑變化 } else if (this.$parent && this.$parent._root) { this._root = this.$parent && this.$parent._root } // 當訪問this.$router時即返回router實例 Object.defineProperty(this, ’$router’, { get() { return this._root._router }, }) // 當訪問this.$route時即返回當前頁面路由信息 Object.defineProperty(this, ’$route’, { get() { return this._root._router.history.current }, }) }, })}export default VueRouter

router-link 和 router-view 組件

VueRouter.install = function (_Vue) { Vue = _Vue Vue.component(’router-link’, { props: { to: { type: [String, Object], required: true, }, tag: { type: String, default: ’a’, }, }, methods: { handleClick(event) { // 阻止a標簽默認跳轉 event && event.preventDefault && event.preventDefault() let mode = this._self._root._router.mode let path = this.to this._self._root._router.history.current = path if (mode === ’hash’) { window.history.pushState(null, ’’, ’#/’ + path.slice(1)) } else { window.history.pushState(null, ’’, path.slice(1)) } }, }, render(h) { let mode = this._self._root._router.mode let tag = this.tag || ’a’ let to = mode === ’hash’ ? ’#’ + this.to : this.to console.log(’render’, this.to) return ( <tag on-click={this.handleClick} href={to}> {this.$slots.default} </tag> ) // return h(tag, { attrs: { href: to }, on: { click: this.handleClick } }, this.$slots.default) }, }) Vue.component(’router-view’, { render(h) { let current = this._self._root._router.history.current // current已經是動態響應 let routesMap = this._self._root._router.routesMap return h(routesMap[current]) // 動態渲染對應組件 }, })}

至此,一個簡易的vue-router就實現完了,案例完整代碼附上:

let Vue = nullclass HistoryRoute { constructor() { this.current = null }}class VueRouter { constructor(options) { this.mode = options.mode || ’hash’ this.routes = options.routes || [] this.routesMap = this.createMap(this.routes) this.history = new HistoryRoute() // 當前路由 // 初始化路由函數 this.initRoute() } createMap(routes) { return routes.reduce((pre, current) => { pre[current.path] = current.component return pre }, {}) } initRoute() { if (this.mode === ’hash’) { // 先判斷用戶打開時有沒有hash值,沒有的話跳轉到 #/ location.hash ? ’’ : (location.hash = ’/’) window.addEventListener(’load’, () => { this.history.current = location.hash.slice(1) }) window.addEventListener(’hashchange’, () => { this.history.current = location.hash.slice(1) }) } else { // history模式 location.pathname ? ’’ : (location.pathname = ’/’) window.addEventListener(’load’, () => { this.history.current = location.pathname }) window.addEventListener(’popstate’, () => { this.history.current = location.pathname }) } }}VueRouter.install = function(_Vue) { Vue = _Vue Vue.mixin({ beforeCreate() { // 根組件 if (this.$options && this.$options.router) { this._root = this // 把當前vue實例保存到_root上 this._router = this.$options.router // 把router的實例掛載在_router上 Vue.util.defineReactive(this, ’_route’, this._router.history) // 監聽history路徑變化 } else if (this.$parent && this.$parent._root) { // 子組件的話就去繼承父組件的實例,讓所有組件共享一個router實例 this._root = this.$parent && this.$parent._root } // 當訪問this.$router時即返回router實例 Object.defineProperty(this, ’$router’, { get() { return this._root._router }, }) // 當訪問this.$route時即返回當前頁面路由信息 Object.defineProperty(this, ’$route’, { get() { return this._root._router.history.current }, }) }, }) Vue.component(’router-link’, { props: { to: { type: [String, Object], required: true, }, tag: { type: String, default: ’a’, }, }, methods: { handleClick(event) { // 阻止a標簽默認跳轉 event && event.preventDefault && event.preventDefault() // 阻止a標簽默認跳轉 let mode = this._self._root._router.mode let path = this.to this._self._root._router.history.current = path if (mode === ’hash’) { window.history.pushState(null, ’’, ’#/’ + path.slice(1)) } else { window.history.pushState(null, ’’, path.slice(0)) } }, }, render(h) { let mode = this._self._root._router.mode let tag = this.tag || ’a’ let to = mode === ’hash’ ? ’#’ + this.to : this.to return ( <tag on-click={this.handleClick} href={to}> {this.$slots.default} </tag> ) // return h(tag, { attrs: { href: to }, on: { click: this.handleClick } }, this.$slots.default) }, }) Vue.component(’router-view’, { render(h) { let current = this._self._root._router.history.current // current已經是動態 let routesMap = this._self._root._router.routesMap return h(routesMap[current]) // 動態渲染對應組件 }, })}export default VueRouter

ps: 個人技術博文 Github 倉庫,覺得不錯的話歡迎 star,給我一點鼓勵繼續寫作吧~

以上就是如何手寫簡易的 Vue Router的詳細內容,更多關于手寫簡易的 Vue Router的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲午夜精品久久久久久app| 国产一区二区三区探花| 日韩视频二区| 六月丁香综合在线视频| 亚洲调教视频在线观看| 9国产精品视频| 高清久久精品| 日韩电影二区| 精品深夜福利视频| 国产亚洲精品美女久久久久久久久久| 亚洲免费观看高清完整版在线观| 亚洲免费一区二区| 国产成人免费| 国产精品亚洲欧美| 鲁大师影院一区二区三区| 在线成人直播| 99日韩精品| 在线日韩av| 日韩午夜一区| 亚洲精品视频一二三区| 91超碰国产精品| 久久91导航| 红桃视频欧美| 成人在线免费观看91| 国产亚洲久久| 国产精品调教| 国产一二在线播放| 欧美特黄一区| 日韩高清在线观看一区二区| 亚洲精品激情| 麻豆视频观看网址久久| 日本а中文在线天堂| 久久国产精品久久w女人spa| 日韩一区精品字幕| 福利一区二区三区视频在线观看| 成人午夜网址| 精品精品久久| 亚洲一区二区免费看| 婷婷久久免费视频| 巨乳诱惑日韩免费av| 深夜福利一区| 国产日韩亚洲| 性色一区二区| 久久69成人| 免费国产亚洲视频| 最新国产拍偷乱拍精品| 天堂成人免费av电影一区| 国产无遮挡裸体免费久久| 国产99久久| 日韩一区二区三区在线看| 国产精久久久| 亚洲午夜黄色| 日本国产欧美| 99热国内精品| 亚洲免费激情| 久久不见久久见中文字幕免费 | 亚洲激情欧美| 国产精品nxnn| 亚洲啊v在线免费视频| 日韩中文字幕av电影| 人人香蕉久久| 成人在线视频区| 天堂精品久久久久| 亚洲一区二区三区四区电影| 日韩黄色在线观看| 久久青草久久| 精品午夜av| 日本va欧美va欧美va精品| 99久精品视频在线观看视频| 欧美一区=区| 欧美一区二区三区激情视频 | 中文字幕av一区二区三区人| 色在线中文字幕| 麻豆精品在线观看| 一区二区国产在线观看| 一区二区三区网站| 亚洲91久久| 国产videos久久| 精品国产亚洲日本| 麻豆精品久久| 国产一区二区亚洲| 国产一区二区三区天码| 欧美激情综合| 精品五月天堂| 国产粉嫩在线观看| 五月天综合网站| 麻豆精品91| 日韩专区一卡二卡| 欧美日韩亚洲三区| 亚洲精品看片| 国产精品网址| 岛国av在线网站| 欧美中文字幕| 亚洲毛片在线| 亚洲精品**中文毛片| 日韩欧美少妇| 日韩三级一区| 精品国产一区二区三区噜噜噜| 国产美女高潮在线观看| 久久精品免费一区二区三区 | 日韩精品免费观看视频| 国产盗摄——sm在线视频| 亚洲第一区色| 亚洲精品高潮| 国产欧美一级| 蜜桃久久久久久久| 国产亚洲精品美女久久| 国产在线视频欧美一区| 亚洲欧美网站在线观看| 日韩精品免费视频人成| 久久av影视| 天堂资源在线亚洲| 蜜臀国产一区二区三区在线播放| 久久精品99久久久| 一级欧洲+日本+国产| 久久99影视| 亚洲视频播放| 欧美影院三区| 国产欧美一区二区精品久久久| 精品一区在线| 91综合视频| 国产一区丝袜| 免费一级欧美片在线观看网站| 性色av一区二区怡红| 性色一区二区| 国产在线不卡| 久久久精品网| 免费一二一二在线视频| 大香伊人久久精品一区二区| 国产精品亚洲欧美一级在线| 日韩精品免费观看视频| 国产精品天堂蜜av在线播放| 婷婷成人av| 亚洲三级精品| 国产精品多人| 伊人精品一区| 综合一区在线| 日韩一区二区三区在线看| 亚洲免费福利一区| 青草国产精品| 精品女同一区二区三区在线观看| 91日韩免费| 日韩久久精品网| 一本大道色婷婷在线| 日韩精品久久久久久久软件91| 日本久久精品| 久久九九精品| 日韩国产一区二| 成人免费电影网址| 999久久久免费精品国产| 欧美高清不卡| 日韩激情精品| sm久久捆绑调教精品一区| 亚洲三级视频| 日韩国产网站| 在线国产日韩| 视频一区中文| 日韩精品久久理论片| 国产精品www994| 在线亚洲激情| 青青草91久久久久久久久| 精品丝袜在线| 青青国产91久久久久久| 精品国产欧美| 免费久久99精品国产自在现线| 国产亚洲高清一区| 99riav国产精品| 激情综合激情| 欧美一级全黄| 青草国产精品| 国产一区欧美| 亚洲一级少妇| 久久丁香四色| 亚洲综合不卡| 亚洲欧洲美洲av| 日韩中文字幕在线一区| 99国产精品| 激情综合五月| 亚洲日本在线观看视频| 国产成人免费| 国产精品视频3p| 久久精品99国产精品| 成人羞羞视频在线看网址| 国产欧美激情| 欧美日本精品| 亚洲精品看片| 亚欧洲精品视频在线观看| 国产在线成人| 首页国产欧美日韩丝袜| 国产不卡人人| 欧美 日韩 国产一区二区在线视频| 国产精品久久久久久模特| 在线视频免费在线观看一区二区| 91久久午夜| 尹人成人综合网| 亚洲一级影院| 国产亚洲在线| 国产三级一区| 免费一级欧美片在线观看网站| 国产精品v日韩精品v欧美精品网站|