vue-router 源碼之實現(xiàn)一個簡單的 vue-router
前言
通過上篇,我們知道前端理由的兩種實現(xiàn)方法,Hash 路由與 History 路由,并且用它們分別實現(xiàn)了一個前端路由。
接下來我們就將 Vue 與 Hash 路由結(jié)合,實現(xiàn)一個非常簡單的 vue-router 吧。
開始實現(xiàn)
想象一下,如果自己實現(xiàn)了一個 vue-router,會怎么去使用呢?參考 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>
這里會有 router-link 和 router-view 兩個組件需要我們來實現(xiàn)。再來看 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'
});
這里會有我們自己定義的組件 Home、Book 和 Movie,并且有它們各自對應(yīng)的路由。我們實現(xiàn)的 VueRouter 跟官方的有些區(qū)別,在 VueRouter 被 new 時是將 Vue 作為參數(shù)傳入,而不是注入掛載到根實例下。
接下來就是 VueRouter 的實現(xiàn)了。
VueRouter
要怎么來實現(xiàn) VueRouter 呢,先提供一下實現(xiàn)的思路:
- 綁定 hashchange 事件,實現(xiàn)前端路由;
- 將傳入的路由和組件做一個路由映射,切換哪個路由即可找到對應(yīng)的組件顯示;
- 需要 new 一個 Vue 實例還做響應(yīng)式通信,當(dāng)路由改變的時候,router-view 會響應(yīng)更新;
- 注冊 router-link 和 router-view 組件。
先創(chuàng)建一個 VueRouter:
class VueRouter {
constructor (Vue, options) {
this.$options = options;
}
}
綁定事件
給 VueRouter 添加一個綁定事件的方法,一旦路由發(fā)生改變,會觸發(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è)置成一張路由映射表,以便于通過路由查找到對應(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 一個新的 Vue 實例,將當(dāng)前路由 current 儲存在其 data 之中,當(dāng)修改了 current 時,router-view 就會自己去更新視圖。
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,一旦更新它,便會更新。
注冊組件
router-link 實際上就是一個 <a> 標(biāo)簽,點擊它便能觸發(fā) hashchange。router-view 會實現(xiàn)一個 render 方法,將當(dāng)前路由對應(yīng)的組件取出,進(jìn)行渲染。
constructor (Vue, options) {
this.initComponent(Vue);
}
// 注冊組件
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);
}
});
}
完整代碼
至此,一個簡單的 vue-router 就出來了,全部代碼是這樣的:
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;
});
}
// 注冊組件
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)聽了 hashchange 事件,再通過 Vue 的 響應(yīng)機制 和 組件,便有了上面實現(xiàn)好了一個 vue-router。
全部源碼參考這里。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
- 淺析前端路由簡介以及vue-router實現(xiàn)原理
- vue-router配合ElementUI實現(xiàn)導(dǎo)航的實例
- Vue-Router2.X多種路由實現(xiàn)方式總結(jié)
- vue.js vue-router如何實現(xiàn)無效路由(404)的友好提示
- 使用Vue-Router 2實現(xiàn)路由功能實例詳解
- 詳解基于vue-router的動態(tài)權(quán)限控制實現(xiàn)方案
- Vue-router 類似Vuex實現(xiàn)組件化開發(fā)的示例
- vue-router實現(xiàn)webApp切換頁面動畫效果代碼
相關(guān)文章
vue中v-for和v-if一起使用之使用compute的示例代碼
這篇文章主要介紹了vue中v-for和v-if一起使用之使用compute的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
一款移動優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue使用sign-canvas實現(xiàn)在線手寫簽名的實例
sign-canvas?一個基于?canvas?開發(fā),封裝于?Vue?組件的通用手寫簽名板(電子簽名板),支持?pc?端和移動端,本文給大家分享Vue使用sign-canvas實現(xiàn)在線手寫簽名,感興趣的朋友一起看看吧2022-05-05

