具有固定参数的Vue-Router别名

Bor*_*nte 5 vue-router vuejs2

我在这里有这个小路由器,基本上我想为我的站点使用默认语言。这意味着,如果您访问www.example.com/about和www.example.com/es/about,则应该看到相同的内容

我以为别名会适合这种情况,但是在尝试访问/ about时遇到错误。

[vue-router] missing param for aliased route with path "/:language/about": Expected "language" to be defined
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为未设置:language参数,有什么办法可以实现我想要的?每当用户点击别名时,是否可以设置固定的:language参数?

这是我的路由器。

[vue-router] missing param for aliased route with path "/:language/about": Expected "language" to be defined
Run Code Online (Sandbox Code Playgroud)

小智 5

使用可选参数(附加?)将解决这个问题。您甚至不需要别名:

router = new Router({
  routes: [
    {
      path: '/:language?/about',
      component: About
    }
  ]
})
Run Code Online (Sandbox Code Playgroud)