Vue 嵌套路由使用总结(推荐)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Vue 嵌套路由使用总结(推荐)

  2021-04-02 我要评论

开发环境

Win 10

node-v10.15.3-x64.msi

下载地址:

https://nodejs.org/en/

需求场景

如下图,我们希望点击导航栏不同菜单时,导航栏下方加载不同的组件,进而展示不同的页面内容 

解决方案

使用动态路由

新建home.vue作为子页面组件

<template>
 <div>
  <h3>home Page</h3>
  <p>home page content</p>
 </div>
</template>
<script>
export default {
 name: "homePage",
};
</script>
<style scoped>
h3 {
 font-size: 30px;
}
</style>

新建nav1.vue作为子页面组件

<template>
 <div>
  <h3>nav1 Page</h3>
  <p>nav1 page content</p>
 </div>
</template>
<script>
export default {
 name: "nav1Page",
};
</script>
<style scoped>
h3 {
 font-size: 30px;
}
</style>
新建index.vue作为父页面
<template>
 <div class="container">
  <div class="nav">
   <ul>
    <li>
     <a @click="clickHome">首页</a>
    </li>
    <li>
     <a @click="clickNav1">导航1</a>
    </li>
   </ul>
  </div>
  <div class="content">
   <router-view></router-view>
  </div>
 </div>
</template>
<script>
export default {
 methods: {
  clickHome() {
   this.$router.push("/index/home");
  },
  clickNav1() {
   this.$router.push("nav1");
  }
 }
};
</script>
<style>
.nav ul {
 width: 100%;
 height: 30px;
 margin: 5px;
 padding: 0;
}
.nav ul li {
 float: left; /*横排显示*/
 list-style-type: none; /*去掉前面的点*/
}
.nav ul li a {
 width: 100px;
 display: block; /*设置为block,width才起作用*/
 height: 28px;
 line-height: 28px;
 background: grey;
 color: #fff;
 margin: 0px 1px;
 font-size: 18px;
 text-align: center;
 text-decoration: none;
}
.nav ul li a:hover {
 width: 100px;
 height: 26px;
 line-height: 28px;
 border: 1px solid red;
 color: red;
 background: #fff;
}
.content {
 position: absolute;
 top: 40px;
 bottom: 0px;
 right: 10px;
 left: 15px;
 background: rgb(176, 207, 180)
}
</style>

说明:

1、

 methods: {
  clickHome() {
   this.$router.push("/index/home");
  },
  clickNav1() {
   this.$router.push("nav1");
  }
 }

点击对应“首页”菜单,“导航1”时分别触发调用这里定义了两个方法clickHome()和clickNav1(),两个方法的实现都是调用this.$router.push(),航到不同的 UR(跳转到不同的页面)。另外,push这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,可以回到之前的页面

需要注意的是,这里给push方法提供的代表路径的字符串。如果该字符串不以“/”打头,则表示相对路径,相对于父级路由的path。如果该字符串以“/”打头,则表示绝对路径的,相对于根路径“/”

例中,触发clickNav1()调用时,提供的路径字符串为“nav1”,为相对路径,父级路由路径为/index,所以点击后跳转的url路径为/index/nav1。

例中,触发clickHome()调用时,提供的路径字符串为“/index/home”,为绝对路径,所以点击后跳转的url路径为/index/home。

2、

<div class="content">
<router-view></router-view>
</div>

这里通过在父页面,即index.vue组件中添加<router-view></router-view>实现动态加载不同组件页面。点击导航菜单时,会自动加载对应的组件,然后替换<router-view>元素为对应的组件内容。

参考链接:

https://router.vuejs.org/zh/guide/essentials/navigation.html

https://router.vuejs.org/zh/guide/essentials/nested-routes.html

修改router/index.js

import Vue from "vue"
import Router from "vue-router"
import index from "@/views/index"
import home from "@/views/home"
import nav1 from "@/views/nav1"
Vue.use(Router)
export default new Router({
 mode: "history",
 routes: [
 {
  path: "/index",
  name: "index",
  component: index,
  children: [
  {
   path: "/index/home",
   name: "home",
   component: home
  },
  {
   path: "nav1",
   name: "nav1",
   component: nav1
  }
  ]
 }
 ]
})

说明:

1、vue路由通过children实现路由嵌套。个人理解,嵌套路由控制内容子组件内容的展示区:实现父组件的内容展示区保持不变,子组件内容展示区动态变化。

2、同this.$router.push(path),这里的path也分相对路径(相对于父级路由的path路径),和绝对路径(相对于“/”)。如上,/index/home为绝对路径,nav1为相对路径(其绝对路径为/index/nav1)。注意,这里path是否为绝对路径,不影响是否嵌套路由,是否嵌套路由,是通过children决定的,只是影响路由匹配。如上,如果path: "nav1",写成path: "/nav1",,那么执行this.$router.push("nav1")时,跳转的url为/index/nav1,将找不到匹配的路由

3、this.$router.push(path) 和这里routes的关系:

个人理解,执行this.$router.push(path)后,程序自动获取需要跳转的绝对url,暂且称之为toPath,然后在routes中进行匹配,如果匹配到则加载对应的组件。

总结

通过router-view实现在当前指定容器中动态加载不同组件,展示不同页面的大致实现思路:

1、 在当前页面(这里称之为父页面).vue文件template模板中的指定位置(“包含”子组件内容的容器),添加<router-view></router-view>元素

2、 router/index.js中给父页面路径所在的路由,添加子路由:子组件的加载url对应的路由

以上所述是小编给大家介绍的Vue 嵌套路由使用总结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们