VUE之Router 如何设置子默认路由

VUE之Router 如何设置子默认路由

在Vue Router中,可以通过在路由配置中的 children 属性来设置子路由的默认路径。你需要在父路由中的 children 数组里定义一个具有 path 属性的对象,并将其设置为 "" 或 "/".

以下是一个简单的例子:

c 复制代码
import Vue from 'vue';
import Router from 'vue-router';
import ParentComponent from './components/ParentComponent';
import DefaultChildComponent from './components/DefaultChildComponent';
 
Vue.use(Router);
 
const router = new Router({
  routes: [
    {
      path: '/parent',
      component: ParentComponent,
      children: [
        {
          // 这里设置子路由的默认路径
          path: "", 
          component: DefaultChildComponent
        },
        {
          path: "child1",
          component: () => import('./components/ChildComponent1')
        },
        {
          path: "child2",
          component: () => import('./components/ChildComponent2')
        }
      ]
    }
  ]
});
 
export default router;

在这个例子中,当用户访问 /parent 时,DefaultChildComponent 将作为默认子路由展示。

注意,子路由的默认路径是通过在 path 属性中设置一个空字符串来定义的。

相关推荐
不可能片场24 分钟前
Electron 发布 正文写入的三条通道与读回口径
前端·electron
程序员大阳27 分钟前
副队长JavaScript教程(66)-本地存储实战:待办事项网页的开发
javascript·json·实战·待办事项·本地存储·案例
jerrywus34 分钟前
让 AI 帮你部署 Vercel,报错了也不用手动搬日志
前端·agent·mcp
补码缩写40 分钟前
11 行 Python 拆 PNG 块,解压后的字节数能手算
前端
coding漫漫长路43 分钟前
PNG压缩先看图是哪一类,先无损再量化多半白做
前端
用户2576807632143 分钟前
搜「洗牙」为什么搜不到「洁治」
前端
费曼学习法1 小时前
Agent 死循环的根因不在模型:错误分类、退避、熔断、快照四层实现
前端
修炼的dance1 小时前
同一张PNG换五种压法,体积差在颜色数上
前端
Lank_M1 小时前
PNG 有损压缩省下的体积,大头不在 deflate
前端
用户老李1 小时前
相机模型与标定自学教程 02:外参 \[R | t] 与完整投影链路
前端