Vue Router 嵌套路由中, 子路由path路径带 ‘/‘ 与不带的区别

嵌套路由配置路由规则,使用children配置项:
import Vue from 'vue'
import VueRouter from 'vue-router'
import PageView from '@/layouts/PageView'
import TabsView from '@/layouts/tabs/TabsView'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: TabsView,
    children: [
      {
        path: '/baseInfo',
        component: PageView,
        children: [
          {
            path: 'home',
            component: () =>
              import('@/pages/baseInfo/home/index.vue'),
          },
          {
            path: '/about',
            component: PageView,
            children: [
              {
                path: '/music',
                component: () =>
                  import(
                    '@/pages/baseInfo/about/music/index.vue'
                  ),
              },
              {
                path: 'video',
                component: () =>
                  import(
                    '@/pages/baseInfo/about/video/index.vue'
                  ),
              },
            ],
          },
        ],
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  base: '/app/',
  publicPath: './',
  routes,
})

export default router
区别:
不带 / :

1、当 baseInfo 的子路由 home 定义的路径 path 没有书写 / ,则 $router.push 跳转路径为:

$router.push('/baseInfo/home')

浏览器地址栏显示: http:xxxxxxxxxx/app/baseInfo/home

2、当 baseInfo 的子路由 about 定义 path 有书写 / ,而它的子路由 video 在定义 path 没有书写 / ,则 $router.push 跳转路径为:

$router.push('/about/video')

浏览器地址栏显示: http:xxxxxxxxxx/app/about/video

带 / :

3、当 baseInfo 的子路由 about 定义 path 前有书写 / ,而它的子路由 music 在定义 path 有书写 / ,则 $router.push 跳转路径为:

$router.push('/music')

浏览器地址栏显示:http:xxxxxxxxxx/app/music

总结:

父级有/子级有/

url只写子级 http://localhost:8080/children 子路由写了/代表绝对路径

父级有/子级没/

url要写父级/子级 http://localhost:8080/parent/childre 子路由没有写/代表的拼接的路径

父级有没有/好像无吊所谓主要看子级,路由跳转同理
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router 是一个官方提供的 Vue.js路由管理器,可以用于构建单页面应用程序。嵌套路由是指在一个路由的组件使用另一个路由。 在 Vue Router ,可以通过在路由配置文件定义嵌套路由嵌套路由配置是以树形结构来组织的,父级路由将会嵌套渲染其路由的组件。 下面是一个示例的路由配置文件,演示了如何使用嵌套路由: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ { path: '/', component: Home, children: [ { path: '', component: Dashboard }, { path: 'about', component: About }, { path: 'products', component: Products, children: [ { path: '', component: ProductList }, { path: ':id', component: ProductDetail } ] } ] } ] const router = new VueRouter({ routes }) export default router ``` 在上面的代码,父级路由 '/' 下包含了三个路由:Dashboard、About 和 Products。而 Products 路由又包含了两个路由:ProductList 和 ProductDetail。 在组件使用嵌套路由时,需要在父级组件使用 `<router-view>` 标签来渲染路由的内容。 ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 在父级组件的模板,通过使用 `<router-view>` 标签,路由的内容将会被渲染在这个位置。 这就是 Vue Router 嵌套路由的基本使用方法。通过嵌套路由,可以更好地组织和管理应用程序的路由结构,实现更复杂的页面布局和导航功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值