vue-router 重定向到默认路径问题

新手上路,请多包涵

当用户导航到根路径时,我想默认到特定页面,即使用时转到 myapp.com 我想将它们重定向到 myapp.com/defaultpage

我目前的代码是

index.js

 import Full from '../containers/Full'
import DefaultView from '../views/DefaultView'

export default new Router({
  mode: 'history',
  linkActiveClass: 'open active',
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    {
      path: '/',
      redirect: '/defaultview',
      name: 'home',
      component: Full,
      children: [
        {
          path: '/defaultview',
          name: 'defaultview',
          component: DefaultView
        },
        {
          path: '*',
          component: NotFoundComponent
        }
    }
]})

正如用户访问 myapp.com 时一样,我得到一个“未找到 404 页面”——即 NotFoundComponent。只有当我输入 myapp.com/defaultview 时,我才能进入正确的页面。

有任何想法吗?

原文由 proximacentauri 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 883
2 个回答

使用子级时删除父级的 url 前缀

例如:将 "/defaultview" 改为 defaultview 去掉父路径组件,所以实际代码应该是这样的

routes: [
    {
      path: '/',
      redirect: '/defaultview',
      name: 'home',
      component: Full,
      children: [
        {
          path: 'defaultview', /* changed */
          name: 'defaultview',
          component: DefaultView
        },
        {
          path: '*',
          component: NotFoundComponent
        }
      ]
    }
];

参考 嵌套路由

原文由 Raj 发布,翻译遵循 CC BY-SA 4.0 许可协议

试试这个代码:

 routes: [
    {
      path: '/',
      redirect: '/defaultview'
    },
    {
      path: '/defaultview',
      name: 'defaultview',
      component: DefaultView
    },
    {
      path: '*',
      component: NotFoundComponent
    }
]

原文由 Adriano Resende 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题