配置子路由

1.单独路由界面(例如:router/other.js)

export default [
  // 登录页面
  {
    path: "/",
    name: "login",
    component: () => import("@/views/login"),
    meta: {
      title: "登录"
    }
  },
  // 首页
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/home"),
    meta: {
      title: "首页"
    }
  }
];

2.主路由:index.js

import Vue from "vue";
import Router from "vue-router";
import other from "./other";
Vue.use(Router);

// 路由配置
const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes: [...other]
});
// 设置页面title
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});
export default router;

Vue配置子路由,可按以下步骤操作: 1. **创建父级路由**:在Vue的路由器实例中定义父级路由。示例代码如下: ```javascript import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); const router = new VueRouter({ routes: [ { path: '/parent', component: ParentComponent, children: [ // 子路由将在这里配置 ] } ] }); ``` 这里创建了一个父级路由`/parent`,并为其预留了子路由配置的位置 [^1]。 2. **在父级路由的`children`数组中配置子路由**:在`children`数组中,为每个子路由定义`path`和`component`。示例代码如下: ```javascript import Vue from 'vue'; import VueRouter from 'vue-router'; import ParentComponent from './components/ParentComponent.vue'; import ChildComponent1 from './components/ChildComponent1.vue'; import ChildComponent2 from './components/ChildComponent2.vue'; Vue.use(VueRouter); const router = new VueRouter({ routes: [ { path: '/parent', component: ParentComponent, children: [ { path: 'child1', component: ChildComponent1 }, { path: 'child2', component: ChildComponent2 } ] } ] }); ``` 上述代码中,`/parent`是父级路由,`child1`和`child2`是其子路由。访问`/parent/child1`时,将加载`ChildComponent1`;访问`/parent/child2`时,将加载`ChildComponent2` [^1]。 3. **在父组件中准备嵌套路由的容器**:在父组件的模板中,需要添加`<router-view>`标签,作为子路由的渲染容器。示例代码如下: ```vue <template> <div> <h1>Parent Component</h1> <!-- 子路由的渲染容器 --> <router-view></router-view> </div> </template> <script> export default { name: 'ParentComponent' }; </script> ``` 这样,子路由对应的组件将在`<router-view>`标签的位置渲染 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值