【问题标题】:Vue children routeVue 子路由
【发布时间】:2021-08-12 22:57:28
【问题描述】:

我正在寻找一种使用 vue-router 加快静态博客开发速度的方法,但我发现将子页面与父路由链接时出现问题。


    const routes = [
      {
        path: '/',
        name: 'Home',
        component: Home
      },
      {
        path: '/about', name: 'About', component: About,
        children: [
          { 
            path: '/test', name: 'How test children link', component: prova }
        ]
    
      }
    ]

如果我尝试去localhost/about/test地址Vue找不到页面,但如果我直接去localhost/test可以到达。

如果我将子路径更改为

    path: '/about/test', name: 'How test children link', component: prova }

它有效,但是,我不明白子路由的实用性。

关于儿童路线我不明白什么?

【问题讨论】:

  • 你检查了 nuxtjs 吗?它使路由超级 ez
  • 设置路由名称参数 $router({name: 'about-short-name-of-route})

标签: javascript vue.js vue-router


【解决方案1】:

你在正确的轨道上。但您必须记住,/ 意味着并且确实是某事。

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about', name: 'About', component: About,
    children: [
      { 
        path: 'test', name: 'AboutChild', component: TestComponent 
      }
    ]
  }
]

因此,在为孩子添加路径时,基本上不要包含/。如果这样做,请添加整个路径,因此在这种情况下:path: '/about/test'

同样重要的是,在 About 组件中有一个名为 <router-view /> 的 html 标记之前,您不会看到 AboutChild 的内容。

【讨论】:

  • 非常感谢!现在正在工作!在这种情况下 / ??
  • mmmhh...是的,我明白你的意思,现在我明白了,事实上我在导航栏中有子内容,还有一件事要解决:D
猜你喜欢
  • 2015-12-23
  • 2021-11-06
  • 2019-02-22
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2020-08-06
相关资源
最近更新 更多