Router

基础

  1. vue 路由的mode(模式)有几种, 分别是什么?在那些环境下运行?
    • hash: 使用 URL hash 值来作路由。支持所有浏览器,包括不支持 HTML5 History Api 的浏览器。

    • history: 依赖 HTML5 History API 和服务器配置。【需要后端支持】

    • abstract: 支持所有 JavaScript 运行环境,如 Node.js 服务器端。如果发现没有浏览器的 API,路由会自动强制进入这个模式。

  2. 路由的使用步骤
    1. 安装 vue-router

    yarn add vue-router -S

    1. 在src目录下创建一个router目录, 里面创建一个index.js文件 , 这个目录就是router的模块
    2. 引入第三方的依赖包, 并注册路由
        import Vue from 'vue'
        import VueRouter from 'vue-router'
      
        Vue.use( VueRouter ) //使用vue-router这个第三方插件
      
      注意: import这个关键字要放在整个文件的上层
    3. 创建了一个router对象实例,并且创建路由表
        const routes = [ 
          {
            path: '/home',
            component: Home
          }//每一个对象就是一个路由
        ]
        const router = new VueRouter({
          routes//路由表  必写的
        })
      
    4. 导出router实例

    export default router

    1. 入口文件main.js中引入路由实例 router , 然后在根实例中注册

        import router from './router/index.js'
        new Vue({
          router,
          render: (h) => App 
        }).$mount('#app')
      
    2. 给路由一个路由展示区域

      1. 如果是以及路由, 那么我们放在App组件中,用一个 router-view 的组件表示
       <router-view />
      
    3. 当页面第一次的打开的时候, 需要做一个重定向, 就是要自动跳转到 /home 这个路由上

          const routes = [
            { //我们要求这个路由的配置要放在路由表的最上方
              path: '/',
              redirect: '/home'
            }
          ]
      
    4. 业务: 错误路由匹配,

          const routes = [
            {
              path: '/',
              redirect: '/home'   //重定向
            },
            {
              path: '/home',
              component: Home
            },
            {
              path: '/list',
              component: List
            },
            {
              path: '/detail',
              component: Detail
            },
            {
              path: '/login',
              component: Login
            },
            {
              path: '/register',
              component: Register
            },
            {
              path: '/user',
              component: User
            },
            {
              path: '/shopcar',
              component: Shopcar
            },
            {
              path: '/error',
              component: Error
            },
            {  //这个就是错误路由匹配, vue规定这个必须放在最下面,它必须将上面的路由全找一遍,找不到才用当前这个
              path: '**',
              redirect: '/error'
            }
          ]
      
    5. vue路由模式的确定 mode

      1. 如果你使用的是 hash , 那么a标签就可以了、
      2. 如果你使用 history , 那么我们最好将a标签改成 router-link 这个组件
        • router-link 这个组件 身上必须要有一个 to 属性
    6. 二级路由

        const routes = [
          {
            path: '/shopcar',
            component: Shopcar,
            children: [
              {
                path: 'jia', //不写  /
                component: jia
              }
            ]
          }
        ]
    
    • 注意: 写好配置之后,不要忘记了, 在对应的一级路由的组件中书写 路由展示区域
    1. 命名路由

      作用: 就是简写路径了

        {
            path: '/shopcar',
            component: Shopcar,
            //子路由 
            children: [
              { 
                path: 'jia', // 容易犯错点  /yyb X 
                component: Yyb,
                name: 'jia' //命名路由
              },
              {
                path: 'lulu',
                component: lulu
              }
            ]
            
          },
      
      • 使用:
          <router-link :to = "{name:'jia'}"/>
        
Logo

前往低代码交流专区

更多推荐