vue-router 源碼之實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vue-router
前言
通過(guò)上篇,我們知道前端理由的兩種實(shí)現(xiàn)方法,Hash 路由與 History 路由,并且用它們分別實(shí)現(xiàn)了一個(gè)前端路由。
接下來(lái)我們就將 Vue 與 Hash 路由結(jié)合,實(shí)現(xiàn)一個(gè)非常簡(jiǎn)單的 vue-router 吧。
開(kāi)始實(shí)現(xiàn)
想象一下,如果自己實(shí)現(xiàn)了一個(gè) vue-router,會(huì)怎么去使用呢?參考 vue-router 官方的使用方式,看看 html 的使用:
<div id="app"> <p> <router-link to="#/">home</router-link> <router-link to="#/book">book</router-link> <router-link to="#/movie">movie</router-link> </p> <router-view></router-view> </div>
這里會(huì)有 router-link 和 router-view 兩個(gè)組件需要我們來(lái)實(shí)現(xiàn)。再來(lái)看 js 的:
const Home = { template: '<div>home</div>' };
const Book = { template: '<div>book</div>' };
const Movie = { template: '<div>movie</div>' };
const routes = [
{ path: '/', component: Home },
{ path: '/book', component: Book },
{ path: '/movie', component: Movie }
];
const router = new VueRouter(Vue, {
routes
});
new Vue({
el: '#app'
});
這里會(huì)有我們自己定義的組件 Home、Book 和 Movie,并且有它們各自對(duì)應(yīng)的路由。我們實(shí)現(xiàn)的 VueRouter 跟官方的有些區(qū)別,在 VueRouter 被 new 時(shí)是將 Vue 作為參數(shù)傳入,而不是注入掛載到根實(shí)例下。
接下來(lái)就是 VueRouter 的實(shí)現(xiàn)了。
VueRouter
要怎么來(lái)實(shí)現(xiàn) VueRouter 呢,先提供一下實(shí)現(xiàn)的思路:
- 綁定 hashchange 事件,實(shí)現(xiàn)前端路由;
- 將傳入的路由和組件做一個(gè)路由映射,切換哪個(gè)路由即可找到對(duì)應(yīng)的組件顯示;
- 需要 new 一個(gè) Vue 實(shí)例還做響應(yīng)式通信,當(dāng)路由改變的時(shí)候,router-view 會(huì)響應(yīng)更新;
- 注冊(cè) router-link 和 router-view 組件。
先創(chuàng)建一個(gè) VueRouter:
class VueRouter {
constructor (Vue, options) {
this.$options = options;
}
}
綁定事件
給 VueRouter 添加一個(gè)綁定事件的方法,一旦路由發(fā)生改變,會(huì)觸發(fā) onHashChange 方法。
constructor (Vue, options) {
this.init();
}
// 綁定事件
init () {
window.addEventListener('load', this.onHashChange.bind(this), false);
window.addEventListener('hashchange', this.onHashChange.bind(this), false);
}
路由映射表
將傳入的 options 設(shè)置成一張路由映射表,以便于通過(guò)路由查找到對(duì)應(yīng)的組件。
constructor (Vue, options) {
this.$options = options;
this.routeMap = {};
this.createRouteMap(this.$options);
}
// 路由映射表
createRouteMap (options) {
options.routes.forEach(item => {
this.routeMap[item.path] = item.component;
});
}
options 之中,路由與組件的關(guān)系:
const routes = [
{ path: '/', component: Home },
{ path: '/book', component: Book },
{ path: '/movie', component: Movie }
];
生成的路由映射表:
this.routeMap = {
'/': Home,
'/book': Book,
'/movie': Movie
};
響應(yīng)
我們需要 new 一個(gè)新的 Vue 實(shí)例,將當(dāng)前路由 current 儲(chǔ)存在其 data 之中,當(dāng)修改了 current 時(shí),router-view 就會(huì)自己去更新視圖。
constructor (Vue, options) {
this.app = new Vue({
data: {
current: '#/'
}
});
}
// 獲取當(dāng)前 hash 串
getHash () {
return window.location.hash.slice(1) || '/';
}
// 設(shè)置當(dāng)前路徑
onHashChange () {
this.app.current = this.getHash();
}
只要在 router-view 里使用到了 this.app.current,一旦更新它,便會(huì)更新。
注冊(cè)組件
router-link 實(shí)際上就是一個(gè) <a> 標(biāo)簽,點(diǎn)擊它便能觸發(fā) hashchange。router-view 會(huì)實(shí)現(xiàn)一個(gè) render 方法,將當(dāng)前路由對(duì)應(yīng)的組件取出,進(jìn)行渲染。
constructor (Vue, options) {
this.initComponent(Vue);
}
// 注冊(cè)組件
initComponent (Vue) {
Vue.component('router-link', {
props: {
to: String
},
template: '<a :href="to" rel="external nofollow" rel="external nofollow" ><slot></slot></a>'
});
const _this = this;
Vue.component('router-view', {
render (h) {
var component = _this.routeMap[_this.app.current];
return h(component);
}
});
}
完整代碼
至此,一個(gè)簡(jiǎn)單的 vue-router 就出來(lái)了,全部代碼是這樣的:
class VueRouter {
constructor (Vue, options) {
this.$options = options;
this.routeMap = {};
this.app = new Vue({
data: {
current: '#/'
}
});
this.init();
this.createRouteMap(this.$options);
this.initComponent(Vue);
}
// 綁定事件
init () {
window.addEventListener('load', this.onHashChange.bind(this), false);
window.addEventListener('hashchange', this.onHashChange.bind(this), false);
}
// 路由映射表
createRouteMap (options) {
options.routes.forEach(item => {
this.routeMap[item.path] = item.component;
});
}
// 注冊(cè)組件
initComponent (Vue) {
Vue.component('router-link', {
props: {
to: String
},
template: '<a :href="to" rel="external nofollow" rel="external nofollow" ><slot></slot></a>'
});
const _this = this;
Vue.component('router-view', {
render (h) {
var component = _this.routeMap[_this.app.current];
return h(component);
}
});
}
// 獲取當(dāng)前 hash 串
getHash () {
return window.location.hash.slice(1) || '/';
}
// 設(shè)置當(dāng)前路徑
onHashChange () {
this.app.current = this.getHash();
}
}
最后
將 Vue 與 Hash 路由結(jié)合,監(jiān)聽(tīng)了 hashchange 事件,再通過(guò) Vue 的 響應(yīng)機(jī)制 和 組件,便有了上面實(shí)現(xiàn)好了一個(gè) vue-router。
全部源碼參考這里。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
- 淺析前端路由簡(jiǎn)介以及vue-router實(shí)現(xiàn)原理
- vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例
- Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)
- vue.js vue-router如何實(shí)現(xiàn)無(wú)效路由(404)的友好提示
- 使用Vue-Router 2實(shí)現(xiàn)路由功能實(shí)例詳解
- 詳解基于vue-router的動(dòng)態(tài)權(quán)限控制實(shí)現(xiàn)方案
- Vue-router 類似Vuex實(shí)現(xiàn)組件化開(kāi)發(fā)的示例
- vue-router實(shí)現(xiàn)webApp切換頁(yè)面動(dòng)畫(huà)效果代碼
相關(guān)文章
Vue3中watch監(jiān)聽(tīng)的五種情況詳解
watch函數(shù)用于偵聽(tīng)某個(gè)值的變化,當(dāng)該值發(fā)生改變后,觸發(fā)對(duì)應(yīng)的處理邏輯,本文將給大家介紹了Vue3中watch監(jiān)聽(tīng)的五種情況,文中通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03
vue中v-for和v-if一起使用之使用compute的示例代碼
這篇文章主要介紹了vue中v-for和v-if一起使用之使用compute的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue實(shí)現(xiàn)大屏頁(yè)面的屏幕自適應(yīng)
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)大屏頁(yè)面的屏幕自適應(yīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Vue使用sign-canvas實(shí)現(xiàn)在線手寫(xiě)簽名的實(shí)例
sign-canvas?一個(gè)基于?canvas?開(kāi)發(fā),封裝于?Vue?組件的通用手寫(xiě)簽名板(電子簽名板),支持?pc?端和移動(dòng)端,本文給大家分享Vue使用sign-canvas實(shí)現(xiàn)在線手寫(xiě)簽名,感興趣的朋友一起看看吧2022-05-05

