vue 默认路由无效

const routes = [
  {
    path:"/HelloWorld",
    component: HelloWorld
  },
  {
    path: "/second",
    component: second
  },
  //默认
  {
    path: '/',
    redirect: HelloWorld
  }
]

一进来总是显示second路由,点击可以切换,设置默认HelloWorld为初始路由无效,怎么回事呢?(初学vue者)

阅读 3k
1 个回答

你没注意看文档,你那个redirect使用不当,一般有这么两种方式:

  • 重定向path
const routes = [
  {
    path:"/HelloWorld",
    component: HelloWorld
  },
  {
    path: "/second",
    component: second
  },
  //默认
  {
    path: '/',
    redirect: '/HelloWorld'
  }
]
  • 重定向name
const routes = [
  {
    path:"/HelloWorld",
    name: 'helloworld',
    component: HelloWorld
  },
  {
    path: "/second",
    component: second
  },
  //默认
  {
    path: '/',
    redirect: { name: 'helloworld' }
  }
]

参考文档:https://router.vuejs.org/zh-c...

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