最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-router 源碼之實現(xiàn)一個簡單的 vue-router

 更新時間:2018年07月02日 14:55:47   作者:cobish  
這篇文章主要介紹了vue-router 源碼之實現(xiàn)一個簡單的 vue-router,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

通過上篇,我們知道前端理由的兩種實現(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)的思路:

  1. 綁定 hashchange 事件,實現(xiàn)前端路由;
  2. 將傳入的路由和組件做一個路由映射,切換哪個路由即可找到對應(yīng)的組件顯示;
  3. 需要 new 一個 Vue 實例還做響應(yīng)式通信,當(dāng)路由改變的時候,router-view 會響應(yīng)更新;
  4. 注冊 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)的組件取出,進行渲染。

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue異步組件使用詳解

    Vue異步組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Vue異步組件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 詳解Vue一個案例引發(fā)「內(nèi)容分發(fā)slot」的最全總結(jié)

    詳解Vue一個案例引發(fā)「內(nèi)容分發(fā)slot」的最全總結(jié)

    這篇文章主要介紹了詳解Vue一個案例引發(fā)「內(nèi)容分發(fā)slot」的最全總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Vue3之父子組件異步props數(shù)據(jù)的傳值方式

    Vue3之父子組件異步props數(shù)據(jù)的傳值方式

    這篇文章主要介紹了Vue3之父子組件異步props數(shù)據(jù)的傳值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • vue3中cookie的詳細(xì)使用過程

    vue3中cookie的詳細(xì)使用過程

    近期做的項目比較雜,涉及到前端框架的工作,遇到了許多問題,記錄一下這個比較棘手的問題,下面這篇文章主要給大家介紹了關(guān)于vue3中cookie的詳細(xì)使用方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Vue實現(xiàn)雙向滑動輸入條

    Vue實現(xiàn)雙向滑動輸入條

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)雙向滑動輸入條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vite?vue3多頁面入口打包以及部署踩坑實戰(zhàn)

    Vite?vue3多頁面入口打包以及部署踩坑實戰(zhàn)

    因為我們公司的項目是多頁面應(yīng)用,不同于傳統(tǒng)單頁面應(yīng)用,一個包就可以了,下面這篇文章主要給大家介紹了關(guān)于Vite?vue3多頁面入口打包以及部署踩坑的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • vue獲取當(dāng)前日期時間(使用moment和new?Date())

    vue獲取當(dāng)前日期時間(使用moment和new?Date())

    在項目開發(fā)中我遇到了日期范圍選擇器,兩種獲取當(dāng)前日期并做處理的寫法,這里記錄一下,下面這篇文章主要給大家介紹了關(guān)于vue獲取當(dāng)前日期時間(使用moment和new?Date())的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Vue?FileManagerPlugin?報錯問題及解決

    Vue?FileManagerPlugin?報錯問題及解決

    這篇文章主要介紹了Vue?FileManagerPlugin?報錯問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue日期時間選擇器組件使用方法詳解

    Vue日期時間選擇器組件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue日期時間選擇器組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • VUE解決 v-html不能觸發(fā)點擊事件的問題

    VUE解決 v-html不能觸發(fā)點擊事件的問題

    今天小編就為大家分享一篇VUE解決 v-html不能觸發(fā)點擊事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論

乌苏市| 舟山市| 龙门县| 满城县| 图们市| 项城市| 榆社县| 乐至县| 鹤峰县| 博罗县| 茌平县| 江达县| 中超| 莫力| 离岛区| 江门市| 渭南市| 苍梧县| 容城县| 凉城县| 湖州市| 修武县| 绥宁县| 光山县| 仁寿县| 温州市| 巩留县| 迭部县| 卢氏县| 乌拉特后旗| 敦化市| 西城区| 托克托县| 东兴市| 桃源县| 汉源县| 登封市| 北海市| 共和县| 余姚市| 翁牛特旗|