Vue Router深扒實(shí)現(xiàn)原理
前置知識(shí):插件、slot插槽、mixins混入、render函數(shù)、運(yùn)行時(shí)和完整版的Vue
回顧Vue Router的核心代碼
// 注冊(cè)插件 // Vue.use() 內(nèi)部調(diào)用傳入對(duì)象的 install 方法 Vue.use(VueRouter) // 創(chuàng)建路由對(duì)象 const router = new VueRouter({ routes: [ { name: 'home', path: '/', component: homeComponent } ] }) // 創(chuàng)建 Vue 實(shí)例,注冊(cè) router 對(duì)象 new Vue({ router, render: h => h(App) }).$mount('#app')
注意,上述版本為Vue Router 3.x,用法與最新版的有所不同,但理解了其核心設(shè)計(jì)思路后,再去手寫Vue4也是得心應(yīng)手。
Vue功能的強(qiáng)大就在于它的插件機(jī)制,use可以接受兩種參數(shù)——函數(shù)和對(duì)象。如果是函數(shù)的話,則會(huì)直接調(diào)用這個(gè)函數(shù),如果是對(duì)象的話,則會(huì)調(diào)用對(duì)象的install方法。
代碼實(shí)現(xiàn)
創(chuàng)建Vue-Router插件
let _Vue = null export default class VueRouter { static install(Vue) { // 1. 判斷插件是否已經(jīng)安裝,如果是的話,直接返回 if (Vue.install.installed) return Vue.install.installed = true // 2.將Vue的構(gòu)造函數(shù)記錄在全局 _Vue = Vue // 3.把創(chuàng)建Vue的實(shí)例傳入的router對(duì)象注入到Vue實(shí)例 _Vue.mixin({ beforeCreate() { // 判斷是否是實(shí)例,如果是實(shí)例就不用添加 if (this.$options.router) { _Vue.prototype.$router = this.$options.router } }, }) } }
構(gòu)造函數(shù)
constructor(options) { this.options = options this.routeMap = {} this.data = _Vue.observable({ current: '/' }) }
將options中的路由規(guī)則存放到routeMap中
// 注意,下面的代碼只考慮了path和component對(duì)應(yīng),name同理,也可加上路由元信息,或者遍歷加上children,但為便捷及個(gè)人水平考慮,并未完整實(shí)現(xiàn) createRouteMap() { // 遍歷所有的路由規(guī)則,把路由股則解析成鍵值對(duì)的形式,存儲(chǔ)到routeMap中 this.options.routes.forEach(route => { this.routeMap[route.path] = route.component }) }
初始化route-link和router-view兩個(gè)組件
initComponent(Vue) { Vue.component('router-link', { props: { to: String }, // template: `<a :href="to" rel="external nofollow" rel="external nofollow" ><slot></slot></a>` render(h) { return h("a", { attrs: { href: this.to }, on: { click: this.clickhandler } }, [this.$slots.default]) }, methods: { clickhandler(e) { history.pushState({}, "", this.to) this.$router.data.current = this.to e.preventDefault(); } }, }) const self = this Vue.component('router-view', { render(h) { const component = self.routeMap[self.data.current] return h(component) } }) }
瀏覽器回退、前進(jìn)時(shí)頁面同時(shí)改變
initEvent(){ window.addEventListener("popstate",()=>{ this.data.current = window.location.pathname }) }
完整代碼
let _Vue = null export default class VueRouter { constructor(options) { this.options = options this.routeMap = {} this.data = _Vue.observable({ current: '/' }) this.init() } static install(Vue) { // 1. 判斷插件是否已經(jīng)安裝,如果是的話,直接返回 if (Vue.install.installed) return Vue.install.installed = true // 2.將Vue的構(gòu)造函數(shù)記錄在全局 _Vue = Vue // 3.把創(chuàng)建Vue的實(shí)例傳入的router對(duì)象注入到Vue實(shí)例 _Vue.mixin({ beforeCreate() { // 判斷是否是實(shí)例,如果是實(shí)例就不用添加 if (this.$options.router) { _Vue.prototype.$router = this.$options.router } }, }) } init() { this.createRouteMap() this.initComponent(_Vue) this.initEvent() } createRouteMap() { // 遍歷所有的路由規(guī)則,把路由股則解析成鍵值對(duì)的形式,存儲(chǔ)到routeMap中 this.options.routes.forEach(route => { this.routeMap[route.path] = route.component }) } initComponent(Vue) { Vue.component('router-link', { props: { to: String }, // template: `<a :href="to" rel="external nofollow" rel="external nofollow" ><slot></slot></a>` render(h) { return h("a", { attrs: { href: this.to }, on: { click: this.clickhandler } }, [this.$slots.default]) }, methods: { clickhandler(e) { history.pushState({}, "", this.to) this.$router.data.current = this.to e.preventDefault(); } }, }) const self = this Vue.component('router-view', { render(h) { const component = self.routeMap[self.data.current] return h(component) } }) } initEvent() { window.addEventListener('popstate', () => { this.data.current = window.location.pathname }) } }
到此這篇關(guān)于Vue Router深扒實(shí)現(xiàn)原理的文章就介紹到這了,更多相關(guān)Vue Router內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于vue.js中this.$emit的理解使用
本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Vue v-model相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Vue ​v-model相關(guān)知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-01-01vue中的vue-print-nb如何實(shí)現(xiàn)頁面打印
這篇文章主要介紹了vue中的vue-print-nb如何實(shí)現(xiàn)頁面打印,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue3中使用Apache?ECharts的詳細(xì)方法
最近在做一些數(shù)據(jù)透析的項(xiàng)目需要用到報(bào)表圖,那么報(bào)表圖好用的有老牌的ECharts,比較新意的AntV,思前馬后的想了一下還是用了Echarts,這篇文章主要介紹了vue3中使用Apache?ECharts,需要的朋友可以參考下2022-11-11vue-cli下的vuex的簡(jiǎn)單Demo圖解(實(shí)現(xiàn)加1減1操作)
這篇文章主要介紹了vue-cli下的vuex的簡(jiǎn)單Demo(實(shí)現(xiàn)加1減1操作),本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02