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

相关推荐
qiao若huan喜4 分钟前
08_React redux
前端·react.js·前端框架
pan_junbiao21 分钟前
Vue3.0组合式API:setup()函数
前端·javascript·vue.js
黄毛火烧雪下22 分钟前
React 中,Hook 是一个特定的概念
前端·javascript·react.js
正小安24 分钟前
Vue 3 中 useRouter 与 useRoute 的深度解析
前端·javascript·vue.js
Missying5527 分钟前
vue前端实现下载导入模板文件
前端·vue.js·elementui·html
计算机学姐27 分钟前
基于python+django+vue+MySQL的酒店推荐系统
开发语言·vue.js·后端·python·mysql·django·pip
洗发水很好用27 分钟前
vue2制作高复用页面
前端·javascript·vue.js
533_31 分钟前
[vue] vue-seamless-scroll 滚动到第二遍的时候不能进行点击的问题
前端·javascript·vue.js
jiaoxingk35 分钟前
Selenium如何通过js注入避免被检测
javascript·selenium·测试工具
2301_7969821436 分钟前
requests-html的详细使用方法
前端·python·html