日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

“约见”面试官系列之常见面试题第二十五篇之对vue-router的理解(建议收藏)

發(fā)布時(shí)間:2023/12/9 vue 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 “约见”面试官系列之常见面试题第二十五篇之对vue-router的理解(建议收藏) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

先整體展示下vue-router使用方式,請牢記一下幾步哦!
?

import Vue from 'vue' import VueRouter from 'vue-router'


//注冊插件 如果是在瀏覽器環(huán)境運(yùn)行的,可以不寫該方法

Vue.use(VueRouter)



// 1. 定義(路由)組件。
// 可以從其他文件 import 進(jìn)來

const User = { template: '<div>用戶</div>' } const Role = { template: '<div>角色</div>' }



// 2. 定義路由
// Array,每個(gè)路由應(yīng)該映射一個(gè)組件。

const routes = [{ path: '/user', component: User },{ path: '/home', component: Home } ]



// 3. 創(chuàng)建 router 實(shí)例,并傳 `routes` 配置

const router = new VueRouter({routes })



// 4. 創(chuàng)建和掛載根實(shí)例。
// 記得要通過 router 對象以參數(shù)注入Vue,
// 從而讓整個(gè)應(yīng)用都有路由功能
// 使用 router-link 組件來導(dǎo)航.
// 路由出口
// 路由匹配到的組件將渲染在這里

const app = new Vue({router,template: `<div id="app"><h1>Basic</h1><ul><li><router-link to="/">/</router-link></li><li><router-link to="/user">用戶</router-link></li><li><router-link to="/role">角色</router-link></li><router-link tag="li" to="/user">/用戶</router-link></ul><router-view class="view"></router-view></div>` }).$mount('#app')



vue-router源碼結(jié)構(gòu)

github 地址:https://github.com/vuejs/vue-router



components下是兩個(gè)組件<router-view> 和 <router-link>

history是路由方式的封裝,提供三種方式

util下主要是各種功能類和功能函數(shù)

create-matcher和create-router-map是生成匹配表

index是VueRouter類,也整個(gè)插件的入口

Install 提供安裝的方法

分析開始:

第一步

Vue是使用.use( plugins )方法將插件注入到Vue中。
use方法會(huì)檢測注入插件VueRouter內(nèi)的install方法,如果有,則執(zhí)行install方法。

注意:如果是在瀏覽器環(huán)境,在index.js內(nèi)會(huì)自動(dòng)調(diào)用.use方法。如果是基于node環(huán)境,需要手動(dòng)調(diào)用。
?

if (inBrowser && window.Vue) {window.Vue.use(VueRouter) }



Install解析 (對應(yīng)目錄結(jié)構(gòu)的install.js)

該方法內(nèi)主要做了以下三件事:

1、對Vue實(shí)例混入beforeCreate鉤子操作(在Vue的生命周期階段會(huì)被調(diào)用)
2、通過Vue.prototype定義router、router、route 屬性(方便所有組件可以獲取這兩個(gè)屬性)
3、Vue上注冊router-link和router-view兩個(gè)組件
?

export function install (Vue) {if (install.installed && _Vue === Vue) returninstall.installed = true_Vue = Vueconst isDef = v => v !== undefinedconst registerInstance = (vm, callVal) => {let i = vm.$options._parentVnodeif (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {i(vm, callVal)}}Vue.mixin({//對Vue實(shí)例混入beforeCreate鉤子操作beforeCreate () {if (isDef(this.$options.router)) {this._routerRoot = thisthis._router = this.$options.routerthis._router.init(this)Vue.util.defineReactive(this, '_route', this._router.history.current)} else {this._routerRoot = (this.$parent && this.$parent._routerRoot) || this}registerInstance(this, this)},destroyed () {registerInstance(this)}})//通過Vue.prototype定義$router、$route 屬性(方便所有組件可以獲取這兩個(gè)屬性)Object.defineProperty(Vue.prototype, '$router', {get () { return this._routerRoot._router }})Object.defineProperty(Vue.prototype, '$route', {get () { return this._routerRoot._route }})//Vue上注冊router-link和router-view兩個(gè)組件Vue.component('RouterView', View)Vue.component('RouterLink', Link)const strats = Vue.config.optionMergeStrategies// use the same hook merging strategy for route hooksstrats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created }



第二步 生成router實(shí)例
?

const router = new VueRouter({routes })



生成實(shí)例過程中,主要做了以下兩件事
1、根據(jù)配置數(shù)組(傳入的routes)生成路由配置記錄表。
2、根據(jù)不同模式生成監(jiān)控路由變化的History對象

注:History類由HTML5History、HashHistory、AbstractHistory三類繼承
history/base.js實(shí)現(xiàn)了基本history的操作
history/hash.js,history/html5.js和history/abstract.js繼承了base,只是根據(jù)不同的模式封裝了一些基本操作

第三步 生成vue實(shí)例
?

const app = new Vue({router,template: `<div id="app"><h1>Basic</h1><ul><li><router-link to="/">/</router-link></li><li><router-link to="/user">用戶</router-link></li><li><router-link to="/role">角色</router-link></li><router-link tag="li" to="/user">/用戶</router-link></ul><router-view class="view"></router-view></div>` }).$mount('#app')




代碼執(zhí)行到這,會(huì)進(jìn)入Vue的生命周期,還記得第一步Vue-Router對Vue混入了beforeCreate鉤子嗎,在此會(huì)執(zhí)行哦
?

Vue.mixin({beforeCreate () {//驗(yàn)證vue是否有router對象了,如果有,就不再初始化了if (isDef(this.$options.router)) { //沒有router對象//將_routerRoot指向根組件this._routerRoot = this//將router對象掛載到根組件元素_router上this._router = this.$options.router//初始化,建立路由監(jiān)控this._router.init(this)//劫持?jǐn)?shù)據(jù)_route,一旦_route數(shù)據(jù)發(fā)生變化后,通知router-view執(zhí)行render方法Vue.util.defineReactive(this, '_route', this._router.history.current)} else {//如果有router對象,去尋找根組件,將_routerRoot執(zhí)行根組件(解決嵌套關(guān)系時(shí)候_routerRoot指向不一致問題)this._routerRoot = (this.$parent && this.$parent._routerRoot) || this}registerInstance(this, this)},destroyed () {registerInstance(this)}})



代碼執(zhí)行到這,初始化結(jié)束,界面將顯示默認(rèn)首頁

路由更新方式:

一、主動(dòng)觸發(fā)

router-link綁定了click方法,觸發(fā)history.push或者h(yuǎn)istory.replace,從而觸發(fā)history.transitionTo。
transitionTo用于處理路由轉(zhuǎn)換,其中包含了updateRoute用于更新_route。
在beforeCreate中有劫持_route的方法,當(dāng)_route變化后,觸發(fā)router-view的變化。

二、地址變化(如:在瀏覽器地址欄直接輸入地址)

HashHistory和HTML5History會(huì)分別監(jiān)控hashchange和popstate來對路由變化作對用的處理 。
HashHistory和HTML5History捕獲到變化后會(huì)對應(yīng)執(zhí)行push或replace方法,從而調(diào)用transitionTo
,剩下的就和上面主動(dòng)觸發(fā)一樣啦。

總結(jié)

1、安裝插件
混入beforeCreate生命周期處理,初始化_routerRoot,_router,_route等數(shù)據(jù)
全局設(shè)置vue靜態(tài)訪問router和router和route,方便后期訪問
完成了router-link和 router-view 兩個(gè)組件的注冊,router-link用于觸發(fā)路由的變化,router-view作 為功能組件,用于觸發(fā)對應(yīng)路由視圖的變化

2、根據(jù)路由配置生成router實(shí)例
根據(jù)配置數(shù)組生成路由配置記錄表
生成監(jiān)控路由變化的hsitory對象

3、將router實(shí)例傳入根vue實(shí)例
根據(jù)beforeCreate混入,為根vue對象設(shè)置了劫持字段_route,用戶觸發(fā)router-view的變化
調(diào)用init()函數(shù),完成首次路由的渲染,首次渲染的調(diào)用路徑是 調(diào)用history.transitionTo方法,根據(jù)router的match函數(shù),生成一個(gè)新的route對象
接著通過confirmTransition對比一下新生成的route和當(dāng)前的route對象是否改變,改變的話觸發(fā)updateRoute,更新hsitory.current屬性,觸發(fā)根組件的_route的變化,從而導(dǎo)致組件的調(diào)用render函數(shù),更新router-view

另外一種更新路由的方式是主動(dòng)觸發(fā)
router-link綁定了click方法,觸發(fā)history.push或者h(yuǎn)istory.replace,從而觸發(fā)history.transitionTo
同時(shí)會(huì)監(jiān)控hashchange和popstate來對路由變化作對用的處理

“睡服“面試官系列之各系列目錄匯總(建議學(xué)習(xí)收藏)?

總結(jié)

以上是生活随笔為你收集整理的“约见”面试官系列之常见面试题第二十五篇之对vue-router的理解(建议收藏)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。