vue-router 重定向到默认路径问题 [英] vue-router redirect to default path issue

查看:155
本文介绍了vue-router 重定向到默认路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to default to a specific page when user navigates to the root path ie when used goes to myapp.com I want to redirect them to 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 才能进入正确的页面.

As it is when user goes to myapp.com I get a '404 page not found' - ie the NotFoundComponent. Only when I type in myapp.com/defaultview can I get to the correct page.

有什么想法吗?

推荐答案

当使用 children 时移除父级的 url 前缀

When using children remove url prefix of the parent

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

ex: change "/defaultview" to defaultview remove the parent path component, so the actual code should be like this

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

参考 嵌套路由

这篇关于vue-router 重定向到默认路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆