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

相关推荐
ZPC82104 小时前
如何创建一个单例类 (Singleton)
开发语言·前端·人工智能
紫_龙5 小时前
最新版vue3+TypeScript开发入门到实战教程之重要详解readonly/shallowReadOnly
前端·javascript·typescript
roamingcode6 小时前
前端 AI Agent 多智能体协作架构:从对抗式排查到工作流解耦
前端·人工智能·架构·agent·team
蓝莓味的口香糖7 小时前
【vue】初始化 Vue 项目
前端·javascript·vue.js
aikongmeng7 小时前
【Ai】Claude Code 初始化引导
javascript
光影少年8 小时前
数组去重方法
开发语言·前端·javascript
我命由我123458 小时前
浏览器的 JS 模块化支持观察记录
开发语言·前端·javascript·css·html·ecmascript·html5
weixin_443478518 小时前
Flutter第三方常用组件包之路由管理
前端·javascript·flutter
武藤一雄8 小时前
C# 异步回调与等待机制
前端·microsoft·设计模式·微软·c#·.netcore
啥都不懂的小小白8 小时前
前端CSS入门详解
前端·css