這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)vue-router,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

開(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。
關(guān)于“如何實(shí)現(xiàn)vue-router”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
分享標(biāo)題:如何實(shí)現(xiàn)vue-router-創(chuàng)新互聯(lián)
分享URL:http://chinadenli.net/article38/hjjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、App開(kāi)發(fā)、網(wǎng)站營(yíng)銷(xiāo)、動(dòng)態(tài)網(wǎng)站、小程序開(kāi)發(fā)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容