文章
问答
冒泡
vue-router重定向

每日一小点

路由重定向

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  }
]

访问路劲'/'会重定向到'/home里面去'/home'对应的视图。

除了path,也可以使用name进行重定向,这个与导航相似不在重复;

redirect接受一个函数的时候:

  {
    path: '/',
    redirect: (to) => {
      const { params, query } = to;
      if(qurry?.path) {
        return '/' + query.path;
      }
    } 
  },

这样表示如果后面有路由参数就重定向到路由path参数指定的路劲中,这里没对path不存在的时候做处理,根据需要设置个默认返回的路劲即可。


关于作者

xlynn
获得点赞
文章被阅读