位置: IT常识 - 正文
推荐整理分享关于Vue-Router 底层运行逻辑浅析(vue$router),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:vue router底层原理,vue-router baseurl,vue中routes,vue router addroutes,vue-routet,vue-routet,vue router底层原理,vue $route,内容如对您有帮助,希望把文章链接给更多的朋友!
只要是使用Vue 开发程序 就不可避免的会使用到 Vue-Router ( 路由),作为Vue生态的一部分 Vue-Router 不同于传统的开发模式,每个url都会重新加载页面,而是单页面(Single Page Application)模式是根据url加载不同的组件,这样做可以
1.监听Url的变化,并在变化前后执行相应的代码逻辑2.不同的Url 可以对应不同的组件3.提供了更多方式改变Url的API (Url的改变不能导致页面的刷新)2.Vue-Router 的使用方式1.Vue是使用.use( plugins )方法将插件注入到Vue中。use方法会检测注入插件VueRouter内的install方法,如果有,则执行install方法。注意:如果是在浏览器环境,在index.js内会自动调用.use方法。如果是基于node环境,需要手动调用。所以在浏览器环境下可以省略不写Vue.use(VueRouter) 注册方法。
2.Install解析 (对应目录结构的install.js)该方法内主要做了以下三件事:
1、对Vue实例混入beforeCreate钩子操作(在Vue的生命周期阶段会被调用)2、通过Vue.prototype定义router、router、route 属性(方便所有组件可以获取这两个属性)3、Vue上注册router-link和router-view两个组件点击查看代码export function install (Vue) { if (install.installed && _Vue === Vue) return install.installed = true _Vue = Vue const isDef = v => v !== undefined const registerInstance = (vm, callVal) => { let i = vm.$options._parentVnode if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) { i(vm, callVal) } } Vue.mixin({ //对Vue实例混入beforeCreate钩子操作 beforeCreate () { if (isDef(this.$options.router)) { this._routerRoot = this this._router = this.$options.router this._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 属性(方便所有组件可以获取这两个属性) 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两个组件 Vue.component('RouterView', View) Vue.component('RouterLink', Link) const strats = Vue.config.optionMergeStrategies // use the same hook merging strategy for route hooks strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created}3.生成router实例生成实例过程中,主要做了以下两件事
1、根据配置数组(传入的routes)生成路由配置记录表。2、根据不同模式生成监控路由变化的History对象注:History类由HTML5History、HashHistory、AbstractHistory三类继承history/base.js实现了基本history的操作history/hash.js,history/html5.js和history/abstract.js继承了base,只是根据不同的模式封装了一些基本操作
const router = new VueRouter({ routes })
4.生成Vue实例点击查看代码 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的生命周期,在这一步Vue-Router对Vue混入了beforeCreate钩子会在此会执行点击查看代码Vue.mixin({ beforeCreate () { //验证vue是否有router对象了,如果有,就不再初始化了 if (isDef(this.$options.router)) { //没有router对象 //将_routerRoot指向根组件 this._routerRoot = this //将router对象挂载到根组件元素_router上 this._router = this.$options.router //初始化,建立路由监控 this._router.init(this) //劫持数据_route,一旦_route数据发生变化后,通知router-view执行render方法 Vue.util.defineReactive(this, '_route', this._router.history.current) } else { //如果有router对象,去寻找根组件,将_routerRoot执行根组件(解决嵌套关系时候_routerRoot指向不一致问题) this._routerRoot = (this.$parent && this.$parent._routerRoot) || this } registerInstance(this, this) }, destroyed () { registerInstance(this) } })执行完这一步后,代码完成初始化,加载页面完成,界面将默认显示主页3.路由更新方式1.主动触发router-link绑定了click方法,触发history.push或者history.replace,从而触发history.transitionTo。transitionTo用于处理路由转换,其中包含了updateRoute用于更新route。在beforeCreate中有劫持route的方法,当_route变化后,触发router-view的变化。
2.地址变化(如:在浏览器地址栏直接输入地址)HashHistory和HTML5History会分别监控hashchange和popstate来对路由变化作对用的处理 。HashHistory和HTML5History捕获到变化后会对应执行push或replace方法,从而调用transitionTo,剩下的就和上面主动触发一样
4.总结1、安装插件混入beforeCreate生命周期处理,初始化_routerRoot,_router,_route等数据全局设置vue静态访问router和router和route,方便后期访问完成了router-link和 router-view 两个组件的注册,router-link用于触发路由的变化,router-view作 为功能组件,用于触发对应路由视图的变化2、根据路由配置生成router实例根据配置数组生成路由配置记录表生成监控路由变化的hsitory对象3、将router实例传入根vue实例根据beforeCreate混入,为根vue对象设置了劫持字段_route,用户触发router-view的变化调用init()函数,完成首次路由的渲染,首次渲染的调用路径是 调用history.transitionTo方法,根据router的match函数,生成一个新的route对象接着通过confirmTransition对比一下新生成的route和当前的route对象是否改变,改变的话触发updateRoute,更新hsitory.current属性,触发根组件的route的变化,从而导致组件的调用render函数,更新router-view,另外一种更新路由的方式是主动触发router-link绑定了click方法,触发history.push或者history.replace,从而触发history.transitionTo同时会监控hashchange和popstate来对路由变化作对用的处理友情连接旭逸的博客上一篇:Python代码用在这些地方,其实1行就够了!(python代码用什么运行)
下一篇:python中INF值的介绍(python输出inf)
友情链接: 武汉网站建设