1.Vue-Router
只要是使用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,剩下的就和上面主动触发一样