温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Vue中如何使用嵌套路由

发布时间:2021-07-09 15:54:06 来源:亿速云 阅读:201 作者:Leah 栏目:web开发

今天就跟大家聊聊有关Vue中如何使用嵌套路由,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

解决方案

使用动态路由

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

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

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

<template>
 <div>
  <h4>nav1 Page</h4>
  <p>nav1 page content</p>
 </div>
</template>
<script>
export default {
 name: "nav1Page",
};
</script>
<style scoped>
h4 {
 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中如何使用嵌套路由有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI