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 属性中设置一个空字符串来定义的。

相关推荐
计算机魔术师2 小时前
Ethan Mollick 谈点与群:智能体自组织为何让管理假设失效
前端
想吃火锅10052 小时前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode
故七月2 小时前
GEO 信源评估体系:如何判断一条网页信源能否被大模型采信
前端·网络·人工智能
liangshanbo12152 小时前
Webpack 常见 Plugin 面试题
前端·webpack·node.js
xxwl5852 小时前
vue3的入门学习
前端·vue.js·学习
用户847172102842 小时前
.shot 文件格式规范
前端·后端
web打印社区2 小时前
政务办事大厅:叫号小票、办理回执网页怎么静默出纸
前端·javascript·vue.js·pdf·html·政务
C++ 老炮儿的技术栈3 小时前
有符号变量与无符号变量的区别
服务器·开发语言·前端·数据结构·c++·算法·c
aixingpan3 小时前
aixingpan.cn API开发文档:api_docs_bichart_natalvssecondary2接口指南
前端·php
MZ_ZXD0013 小时前
【关注可白嫖源码】--课程设计--毕业设计-- 29210 Vue.js 3+Springboot中药材基本信息与图片查询系统
javascript·spring boot·课程设计·毕设·大作业·程序开发·毕设代做