vue设置子路由

您可以在原有的路由配置下加入children字段来增加子路由。[{ path : '/' , component :xxx}, { path : 'xx' , component :xxx}]。注意:children里面的path 不要加 / ,加了就表示是根目录下的路由。

Vue子路由简介

Vue.js是一个用于构建用户界面的渐进式框架,它的核心库专注于视图层,易于与其他库或已有项目整合,VueRouter是Vue.js官方的路由管理器,它允许我们实现单页面应用(SPA)的功能,如嵌套页面、懒加载等,在VueRouter中,我们可以通过配置路由规则来实现子路由的功能。

如何创建子路由

1、我们需要安装并引入VueRouter:

vue设置子路由

npm install vue-router
import Vue from 'vue';
import VueRouter from 'vue-router';

2、我们需要创建一个VueRouter实例,并配置路由规则,在这个例子中,我们将创建一个名为Parent的父组件,它包含两个子组件:Child1Child2,我们将在父组件中定义两个子路由:

Vue.use(VueRouter);
const Parent = {
  components: {
    Child1: () => import('./components/Child1.vue'),
    Child2: () => import('./components/Child2.vue'),
  },
};

3、接下来,我们需要定义路由规则,在这个例子中,我们将为Child1Child2分别设置不同的路径,我们可以将Child1的路径设置为/child1,将Child2的路径设置为/child2:

const routes = [
  { path: '/child1', component: Parent.Child1 },
  { path: '/child2', component: Parent.Child2 },
];

4、我们需要创建一个VueRouter实例,并将路由规则传递给它:

const router = new VueRouter({
  mode: 'history', // 使用HTML5的历史模式
  base: process.env.BASE_URL, // 设置基础URL,可以是绝对路径或相对路径
  routes, // 将路由规则传递给VueRouter实例
});

5、将VueRouter实例挂载到父组件中:

vue设置子路由

export default Parent;

子路由的使用与跳转

1、在父组件中使用子组件:

<template>
  <div>
    <h2>Child1</h2>
    <Child1 />
    <h2>Child2</h2>
    <Child2 />
  </div>
</template>

2、在子组件中使用<router-view>标签来显示当前路由对应的组件内容:

<template>
  <div>这是子组件的内容</div>
</template>

3、实现子路由的跳转:

通过编程式导航实现跳转:通过改变当前激活的路由来实现跳转,我们可以在某个方法中调用this.$router.push()来实现跳转:

vue设置子路由

methods: {
  goToChild1() {
    this.$router.push('/child1');
  },
},

通过声明式导航实现跳转:通过修改当前激活的路由来实现跳转,我们可以在模板中使用<router-link>标签来实现跳转:

<router-link to="/child1">跳转到子路由1</router-link> | <router-link to="/child2">跳转到子路由2</router-link> | <router-link to="/">返回首页</router-link> | <router-link to="/child1" tag="button">直接跳转到子路由1</router-link> | <router-link to="/child2" tag="button">直接跳转到子路由2</router-link> | <router-link to="/" tag="button">直接返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过编程式导航跳转到子路由1</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过编程式导航跳转到子路由2</router-link> | <router-link to="/" tag="button" @click="goToHome">通过编程式导航返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航跳转到子路由1</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航跳转到子路由2</router-link> | <router-link to="/" tag="button" @click="goToHome">通过声明式导航返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航直接跳转到子路由1</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航直接跳转到子路由2</router-link> | <router-link to="/" tag="button" @click="goToHome">通过声明式导航直接返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航触发事件跳转到子路由1</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航触发事件跳转到子路由2</router-link> | <router-link to="/" tag="button" @click="goToHome">通过声明式导航触发事件返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航触发事件直接跳转到子路由1</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航触发事件直接跳转到子路由2</router-link> | <router-link to="/" tag="button" @click="goToHome">通过声明式导航触发事件直接返回首页</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航触发事件直接跳转到子路由1并关闭当前页面</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航触发事件直接跳转到子路由2并关闭当前页面</router-link> | <router-link to="/" tag="button" @click="goToHome">通过声明式导航触发事件直接返回首页并关闭当前页面</router-link> | <router-link to="/child1" tag="button" @click="goToChild1">通过声明式导航触发事件直接跳转到子路由1并关闭所有页面</router-link> | <router-link to="/child2" tag="button" @click="goToChild2">通过声明式导航触发事件直接跳转到子路由2并关闭所有页面</router-开关 >

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/190621.html

(0)
K-seoK-seoSEO优化员
上一篇 2024年1月2日 05:46
下一篇 2024年1月2日 05:48

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入