首页 > 解决方案 > 我可以在 Vue 的路由器中为特定路由设置多个别名吗?

问题描述

事情是这样的,虽然 alias 现在完美地满足了我的需求,但我想知道如何为路径声明多个别名,那么,这样的事情会起作用吗?例子:

export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      alias: ['/home', '/home2', '/homeN']
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('./views/About.vue')
    }
  ]
})  

我的意思是,这是推荐的方法吗?Vue路由器中是否有更好的做法?

标签: vuejs2vue-router

解决方案


这很好,他们甚至有一个官方的例子

const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes: [
    { path: '/root', component: Root, alias: '/root-alias' },
    { path: '/home', component: Home,
      children: [
        // absolute alias
        { path: 'foo', component: Foo, alias: '/foo' },
        // relative alias (alias to /home/bar-alias)
        { path: 'bar', component: Bar, alias: 'bar-alias' },
        // multiple aliases
        { path: 'baz', component: Baz, alias: ['/baz', 'baz-alias'] },
        // default child route with empty string as alias.
        { path: 'default', component: Default, alias: '' },
        // nested alias
        { path: 'nested', component: Nested, alias: 'nested-alias',
          children: [
            { path: 'foo', component: NestedFoo }
          ]
        }
      ]
    }
  ]
});

如果您更担心拼写错误,那么您可能只在*通配符路径上使用导航防护,该路径基于路由路径的子字符串进行重定向。


推荐阅读