欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue Router深扒實現(xiàn)原理

 更新時間:2022年09月09日 09:21:50   作者:ExMaterial  
在看這篇文章的幾點要求:需要你先知道Vue-Router是個什么東西,用來解決什么問題,以及它的基本使用。如果你還不懂的話,建議上官網(wǎng)了解下Vue-Router的基本使用后再回來看這篇文章

Vue Router官網(wǎng)

前置知識:插件、slot插槽、mixins混入、render函數(shù)、運行時和完整版的Vue

回顧Vue Router的核心代碼

// 注冊插件
// Vue.use() 內(nèi)部調(diào)用傳入對象的 install 方法
Vue.use(VueRouter)
// 創(chuàng)建路由對象
const router = new VueRouter({
  routes: [
    { name: 'home', path: '/', component: homeComponent }
  ]
})
// 創(chuàng)建 Vue 實例,注冊 router 對象
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

注意,上述版本為Vue Router 3.x,用法與最新版的有所不同,但理解了其核心設計思路后,再去手寫Vue4也是得心應手。

Vue功能的強大就在于它的插件機制,use可以接受兩種參數(shù)——函數(shù)和對象。如果是函數(shù)的話,則會直接調(diào)用這個函數(shù),如果是對象的話,則會調(diào)用對象的install方法。

代碼實現(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的實例傳入的router對象注入到Vue實例
    _Vue.mixin({
      beforeCreate() {
        // 判斷是否是實例,如果是實例就不用添加
        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對應,name同理,也可加上路由元信息,或者遍歷加上children,但為便捷及個人水平考慮,并未完整實現(xiàn)
createRouteMap() {
  // 遍歷所有的路由規(guī)則,把路由股則解析成鍵值對的形式,存儲到routeMap中
  this.options.routes.forEach(route => {
    this.routeMap[route.path] = route.component
  })
}

初始化route-link和router-view兩個組件

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
    })
}

完整代碼

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的實例傳入的router對象注入到Vue實例
    _Vue.mixin({
      beforeCreate() {
        // 判斷是否是實例,如果是實例就不用添加
        if (this.$options.router) {
          _Vue.prototype.$router = this.$options.router
        }
      },
    })
  }
  init() {
    this.createRouteMap()
    this.initComponent(_Vue)
    this.initEvent()
  }
  createRouteMap() {
    // 遍歷所有的路由規(guī)則,把路由股則解析成鍵值對的形式,存儲到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深扒實現(xiàn)原理的文章就介紹到這了,更多相關(guān)Vue Router內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue基于NUXT的SSR詳解

    Vue基于NUXT的SSR詳解

    這篇文章主要介紹了Vue基于NUXT的SSR詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 關(guān)于vue.js中this.$emit的理解使用

    關(guān)于vue.js中this.$emit的理解使用

    本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Vue ​v-model相關(guān)知識總結(jié)

    Vue ​v-model相關(guān)知識總結(jié)

    這篇文章主要介紹了Vue &#8203;v-model相關(guān)知識總結(jié),幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下
    2021-01-01
  • 詳解vuex狀態(tài)管理模式

    詳解vuex狀態(tài)管理模式

    這篇文章主要介紹了詳解vuex狀態(tài)管理模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • vue中的vue-print-nb如何實現(xiàn)頁面打印

    vue中的vue-print-nb如何實現(xiàn)頁面打印

    這篇文章主要介紹了vue中的vue-print-nb如何實現(xiàn)頁面打印,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue實現(xiàn)底部菜單功能

    vue實現(xiàn)底部菜單功能

    本文通過實例代碼給大家介紹了vue實現(xiàn)底部菜單功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-07-07
  • Vue實現(xiàn)簡易跑馬燈效果

    Vue實現(xiàn)簡易跑馬燈效果

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡易跑馬燈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • vue3中使用Apache?ECharts的詳細方法

    vue3中使用Apache?ECharts的詳細方法

    最近在做一些數(shù)據(jù)透析的項目需要用到報表圖,那么報表圖好用的有老牌的ECharts,比較新意的AntV,思前馬后的想了一下還是用了Echarts,這篇文章主要介紹了vue3中使用Apache?ECharts,需要的朋友可以參考下
    2022-11-11
  • 淺談vuex之mutation和action的基本使用

    淺談vuex之mutation和action的基本使用

    本篇文章主要介紹了淺談vuex之mutation和action的基本使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • vue-cli下的vuex的簡單Demo圖解(實現(xiàn)加1減1操作)

    vue-cli下的vuex的簡單Demo圖解(實現(xiàn)加1減1操作)

    這篇文章主要介紹了vue-cli下的vuex的簡單Demo(實現(xiàn)加1減1操作),本文圖文并茂給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02

最新評論