• main.js new Vue 中配置router
    • 注入$route
      • 路由规则
      • 存储当前路由的数据
    • 注入$router
      • 路由对象
        • 包括路由方法
        • currentRoute获取当前的路由规则
  • 动态路由传参
    • 1.路由配置 使用/:id 站位
    • 使用两种方式
      • 1.直接通过当前路由规则
      • 2.通过开启props:true,会将值传递给组件props中 推荐
  • 编程是导航
    • this.$router.push() 传入路径或name
      • 传参 push({name:”123”,params:this.id})
    • this.$router.replace()参数同Push replace不会记录历史
    • this.go()
  • hash和history模式
    • hash模式带#
      • hash模式基于锚点,和onhashchange事件
    • history模式url不带#
      • 改变浏览器地址,不发请求
      • 基于html5中的historyAPI
      • history.pushState() IE10之后
      • history.replaceState()
  • history模式使用
    • 需要服务器支持
      • 单页面应用中,不存在 /login这样的地址
      • 在服务端配置除了静态资源外都返回单页应用外的index.html
      • node中 使用 .use(history)
      • nginx中
        • 在server中配置
          • location / 中
            • try_files:$uri 查找当前文件,未找到的话把它当成目录 $uri/继续查找 未找到 /index.html返回单页面应用的首页
  • Vue-router的实现原理

    • Hash模式
      • 后边的内容作为路径地址

        • 浏览器不会请求
        • 记录到浏览器访问历史中
      • 监听hashchange事件
      • 根据当前的地址找到对应的组件重新渲染
    • History模式

      • 调用history.pushState方法改变地址栏 不发送请求,并添加到历史中
      • 监听popstate事件。pushState replaceState不触发
      • 根据地址找到组件渲染 ```javascript let _Vue = null export default class VueRouter { //静态方法,两个参数,Vue构造函数,可选参数 static install(Vue) { // 1.判断当前插件是否已经被安装 if (VueRouter.install.installed) {

        1. return

        } VueRouter.install.installed = true // 2.把Vue构造函数记录到全局变量 _Vue = Vue // 3.创建Vue实例的时候传入的router注入到Vue实例上 // 混入 _Vue.mixin({

        1. beforeCreate() {
        2. if (this.$options.router) {
        3. _Vue.prototype.$router = this.$options.router
        4. this.$options.router.init()
        5. }
        6. },

        }) } constructor(options) { //记录传入的选项 this.options = options //解析路由规则 this.routeMap = {} //记录当前路由地址,响应式 this.data = _Vue.observable({

        1. current: "/",

        }) } init() {

        this.createRouterMap() this.initComponents(_Vue) this.initEvent() } createRouterMap() { //遍历所有路由规则并将路由规则转化为键值对存储到routeMap中 this.options.routes.forEach((route) => {

        1. this.routeMap[route.path] = route.component

        }) } initComponents(Vue) { // 创建route-link组件 Vue.component(“router-link”, {

        1. props: {
        2. to: String,
        3. },
        4. // template: '<a :href="to"><slot></slot></a>',
        5. //运行时使用render函数
        6. render(h) {
        7. // h创建虚拟dom,创建点击事件阻止默认事件
        8. return h(
        9. "a",
        10. {
        11. attrs: {
        12. href: this.to,
        13. },
        14. on: {
        15. click: this.clickHandler,
        16. },
        17. },
        18. [this.$slots.default]
        19. )
        20. },
        21. methods: {
        22. clickHandler(e) {
        23. history.pushState({}, "", this.to)
        24. this.$router.data.current = this.to
        25. e.preventDefault()
        26. },
        27. },

        }) const self = this Vue.component(“router-view”, {

        1. render(h) {
        2. //获取当前组件
        3. const component = self.routeMap[self.data.current]
        4. return h(component)
        5. },

        }) } initEvent() { window.addEventListener(“popstate”, () => {

        1. this.data.current = window.location.pathname

        }) } }

```