头图

重定向

image.png

访问 /, 重定向到 /about

image.png

  • redirect 返回对象

image.png

image.png

  • 方法
    image.png

image.png

别名

image.png

image.png

  • 多个别名

image.png

image.png

  • index.js

    // 1. 定义路由组件.
    // 也可以从其他文件导入
    import Home from "../views/Home.vue";
    import About from "../views/About.vue";
    import User from "../views/User.vue";
    import NotFound from "../views/NotFound.vue";
    import News from "../views/News.vue";
    import Parent from "../views/Parent.vue";
    import Styleone from "../views/Styleone.vue";
    import Styletwo from "../views/Styletwo.vue";
    import Page from "../views/Page.vue";
    import ShopTop from "../views/ShopTop.vue";
    import ShopMain from "../views/ShopMain.vue";
    import ShopFooter from "../views/ShopFooter.vue";
    
    import {createRouter, createWebHashHistory} from "vue-router";
    
    
    // 2. 定义一些路由
    // 每个路由都需要映射到一个组件。
    // 我们后面再讨论嵌套路由。
    const routes = [
      {
          path: '/',
          // redirect: {name:"about"}
          //redirect: '/about'
          redirect:(to)=>{
              console.log(to);
              return {path:"/about"}
          }
      },
      {
          path: '/about',
          name: 'about',
          component: About
      },
      { path: '/user/:id', component: User },
      {
          name: "news",
          //path: '/news/:id(\\d+)',//正则匹配
          // path: '/news/:id+',//多个参数
          //path: '/news/:id+',//参数可有可无
          //path: '/news/:id*',//参数可重复叠加
          path: '/news/:id?',//参数不可重复叠加
          component: News
      },
      {
          path: '/:path(.*)',
          component: NotFound
      },//使用正则,匹配任意path
      {
          path: "/parent",
          // alias: '/father', //起一个别名
          alias: ['/father', '/laofuqin'], //起多个别名
          component: Parent,
          children: [
              {
                  path: "styleone",
                  component: Styleone
              },
              {
                  path: "styletwo",
                  component: Styletwo
              }
          ]
      },
      {
          path: "/page",
          component: Page
      },
      {
        path: "/shop",
        components: {
            default: ShopMain,
            ShopTop:ShopTop,
            ShopFooter:ShopFooter,
            ShopMain:ShopMain
        }
      }
    ]
    
    // 3. 创建路由实例并传递 `routes` 配置
    // 你可以在这里输入更多的配置,但我们在这里
    // 暂时保持简单
    const router = createRouter({
      // 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
      history: createWebHashHistory(),
      routes, // `routes: routes` 的缩写
    })
    
    
    export default router
    
    

锅包肉
97 声望17 粉丝

这个人很懒,没有什么说的。


引用和评论

0 条评论