重定向和别名

重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b:

const router = new VueRouter({
  routes: [{ path: "/a", redirect: "/b" }],
});
1
2
3

重定向的目标也可以是一个命名的路由:

const router = new VueRouter({
  routes: [{ path: "/a", redirect: { name: "foo" } }],
});
1
2
3

甚至是一个方法,动态返回重定向目标:

const router = new VueRouter({
  routes: [
    {
      path: "/a",
      redirect: (to) => {
        // 方法接收 目标路由 作为参数
        // return 重定向的 字符串路径/路径对象
      },
    },
  ],
});
1
2
3
4
5
6
7
8
9
10
11

别名

解释

“重定向”的意思是,当用户访问 /a 时,URL 将会被替换成 /b,然后匹配路由为 /b,那么“别名”又是什么呢?

/a 的别名是 /b,意味着,当用户访问 /b 时,URL 会保持为 /b,但是路由匹配则为 /a,就像用户访问 /a 一样。 我的理解就是,访问/a 就是当前,你访问/b 显示的还是我这个/a,但是地址栏显示的是/b

const router = new VueRouter({
  routes: [{ path: "/a", component: A, alias: "/b" }],
});
1
2
3
Last Updated:
Contributors: websong