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

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

到此這篇關于Vue Router深扒實現(xiàn)原理的文章就介紹到這了,更多相關Vue Router內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談vue 組件中的setInterval方法和window的不同

    淺談vue 組件中的setInterval方法和window的不同

    這篇文章主要介紹了淺談vue 組件中的setInterval方法和window的不同,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 在Vue頁面中如何更優(yōu)雅地引入圖片詳解

    在Vue頁面中如何更優(yōu)雅地引入圖片詳解

    我們在Vue.js項目中經(jīng)常需要引用圖片,所以下面這篇文章主要介紹了關于在Vue頁面中如何更優(yōu)雅地引入圖片的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • Vue el-table表格第一列序號與復選框hover切換方式

    Vue el-table表格第一列序號與復選框hover切換方式

    這篇文章主要介紹了Vue el-table表格第一列序號與復選框hover切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue 解決data中定義圖片相對路徑頁面不顯示的問題

    vue 解決data中定義圖片相對路徑頁面不顯示的問題

    這篇文章主要介紹了vue 解決data中定義圖片相對路徑頁面不顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 使用json-editor-vue3(Json字段編輯器)

    使用json-editor-vue3(Json字段編輯器)

    文章介紹了如何在Vue3+Ts項目中使用json-editor-vue3插件,并解決了導入過程中遇到的模塊識別問題,通過步驟說明和代碼示例,展示了如何在項目中成功引入和使用該插件,并提供了一些常見參數(shù)和事件說明
    2025-01-01
  • Vue.Draggable實現(xiàn)交換位置

    Vue.Draggable實現(xiàn)交換位置

    這篇文章主要為大家詳細介紹了Vue.Draggable實現(xiàn)交換位置,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue中mixins的工具的封裝方式

    vue中mixins的工具的封裝方式

    這篇文章主要介紹了vue中mixins的工具的封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue實現(xiàn)的下拉框功能示例

    vue實現(xiàn)的下拉框功能示例

    這篇文章主要介紹了vue實現(xiàn)的下拉框功能,涉及vue.js數(shù)據(jù)讀取、遍歷、事件響應等相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • Vue格式化數(shù)據(jù)后切換頁面出現(xiàn)NaN問題及解決

    Vue格式化數(shù)據(jù)后切換頁面出現(xiàn)NaN問題及解決

    這篇文章主要介紹了Vue格式化數(shù)據(jù)后切換頁面出現(xiàn)NaN問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue項目中添加electron的詳細代碼

    vue項目中添加electron的詳細代碼

    這篇文章通過實例代碼給大家介紹了vue項目中添加electron的方法,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-11-11

最新評論

彩票| 通榆县| 甘孜县| 荆州市| 浦城县| 鸡泽县| 高密市| 金塔县| 敖汉旗| 宁陵县| 逊克县| 布拖县| 南木林县| 新沂市| 灌阳县| 陇西县| 邛崃市| 天津市| 介休市| 崇明县| 新郑市| 渝北区| 麻江县| 太白县| 鹿泉市| 深泽县| 宾川县| 富源县| 新津县| 肃宁县| 堆龙德庆县| 万全县| 大厂| 澎湖县| 申扎县| 交口县| 泸溪县| 卫辉市| 宝山区| 手游| 吉林市|