记录vue-router使用嵌套路由,子路由页面不显示问题

#记录vue-router使用嵌套路由,子路由页面不显示问题

App.vue

<template>
  <div>
	<router-link to="/home" tag="button">home页面</router-link>
	<router-view></router-view>
  <div>
<template>

Router/index.js

const Home = () => import('../components/Home')
const HomeNews = () => import('../components/HomeNews')
const HomeMessage = () => import('../components/HomeMessage')
const routes = [
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '/news',
        component: HomeNews
      },
      {
        path: '/message',
        component: HomeMessage
      }
    ]
  }
]

Home.vue

<template>
  <div>
    <h2>我是首页</h2>
    <router-link to="/home/news">新闻</router-link>
    <router-link to="/home/message">消息</router-link>
    <router-view/>
  </div>

</template>

HomeNews.vue

<template>
  <div>
    <ul>
      <li>重大新闻!!!</li>
      <li>重大新闻!!!</li>
      <li>重大新闻!!!</li>
      <li>重大新闻!!!</li>
      <li>重大新闻!!!</li>
    </ul>
  </div>
</template>

HomeMessage.vue

<template>
  <div>
    <ul>
      <li>好消息!!!好消息!!!</li>
      <li>好消息!!!好消息!!!</li>
      <li>好消息!!!好消息!!!</li>
      <li>好消息!!!好消息!!!</li>
      <li>好消息!!!好消息!!!</li>
      <li>好消息!!!好消息!!!</li>
    </ul>
  </div>
</template>

点击子路由【新闻】或【消息】没有反应,经过多次尝试,最终通过删除路由配置中子路由path的第一个斜杠成功运行

修改后的Router/index.js

  {
    path: '/home',
    component: Home,
    children: [
      {
        //子路由地址配置,前不要加斜杠!!!
        path: 'news',
        component: HomeNews
      },
      {
        path: 'message',
        component: HomeMessage
      }
    ]
  }

当前项目vue版本为2.6.14、vue-router版本为3.5.3
有个地方可能容易写错,就是调用地址 /home/news 千万别忘记home前的斜杠哦!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要在一个页面中展示多个组件的时候,可以使用 Vue.js嵌套路由功能。嵌套路由将一个组件作为父级路由,同时在该组件中展示它的路由组件。 在 Vue.js 中,可以使用 `router-view` 和 `router-link` 来实现嵌套路由。`router-view` 是一个路由出口,用于展示当前路由对应的组件,而 `router-link` 则是一个路由链接,用于跳转到指定的路由。 下面是一个简单的嵌套路由示例: ```html <template> <div> <h1>父级页面</h1> <ul> <li> <router-link to="/parent/child1">页面1</router-link> </li> <li> <router-link to="/parent/child2">页面2</router-link> </li> </ul> <router-view></router-view> </div> </template> <script> export default { name: 'Parent', components: {}, } </script> ``` 在上面的代码中,我们定义了一个父级页面组件 `Parent`,并在其中使用了 `router-link` 来跳转到路由。同时,在父级组件中使用了 `router-view` 来展示路由组件。 接下来,我们还需要定义路由组件。这里我们定义了两个路由组件 `Child1` 和 `Child2`,它们分别对应 `/parent/child1` 和 `/parent/child2` 路由路径。 ```html <template> <div> <h2>{{ title }}</h2> <p>{{ content }}</p> </div> </template> <script> export default { name: 'Child1', data() { return { title: '页面1', content: '这是页面1的内容', } }, } </script> ``` ```html <template> <div> <h2>{{ title }}</h2> <p>{{ content }}</p> </div> </template> <script> export default { name: 'Child2', data() { return { title: '页面2', content: '这是页面2的内容', } }, } </script> ``` 最后,我们需要在路由配置中声明父级路由路由: ```js const routes = [ { path: '/parent', name: 'Parent', component: Parent, children: [ { path: 'child1', name: 'Child1', component: Child1, }, { path: 'child2', name: 'Child2', component: Child2, }, ], }, ] ``` 在上面的路由配置中,我们声明了 `/parent` 路由作为父级路由,同时它有两个路由 `/parent/child1` 和 `/parent/child2`。当用户访问 `/parent/child1` 或 `/parent/child2` 路由时,会分别渲染 `Child1` 和 `Child2` 组件,并在父级页面中展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值