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

node-v10.15.3-x64.msi

下载地址:

https://nodejs.org/en/

需求场景

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

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

解决方案

使用动态路由

新建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> <div> <ul> <li> <a @click="clickHome">首页</a> </li> <li> <a @click="clickNav1">导航1</a> </li> </ul> </div> <div> <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> <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对应的路由

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0d05ddc685b78bcad1588d0b7e296558.html