vue 路由嵌套 及 router-view vue-router --》children
vue 路由嵌套 vue-router --》children
在项目的很多子页面中,我们往往需要在同一个页面做一个组件的切换,同时保存这个页面的部分数据(比如树形菜单),进而显示不同的数据,之前我都是通过v-show/v-if来实现,但当切换的组件太多时,上述方法显然不太合理,而在实际开发中,当你切换的组件太多时,后端往往会将你切换组件的路由给你,所以在这说一下关于vue-router中children,来解决此问题。
例如:在routerChildren.vue中有两个按钮,点击按钮1跳转的one页面 ,点击按钮2跳转的two页面 ,但是需要保存这两个按钮(如果直接通过this.$router.push(),按钮将会消失,会完全跳转)
1.首先我们需要配置一下路由 ,在router.js中

import RouterChildren from "./views/routerChildren.vue"
import RouterChildrenOne from "./views/children/one.vue"
import RouterChildrenTwo from "./views/children/two.vue" {
path: "/routerChildren",
name: "routerChildren",
component: RouterChildren,
children: [
{
path: '/', //代表 /routerChildren
name: "routerChildren",
redirect:'/routerChildren/one' //当我们访问存在子路由的页面时,重定向为第一个子路由,切记,如果写了component,那么这个component也将显示
},
{
path: '/routerChildren/one',
component: RouterChildrenOne,
},
{
path: '/routerChildren/two',
component: RouterChildrenTwo,
}
]
}

2.在rouertChildren.vue组件中,我们写一下简单的样式及跳转

<template>
<div>
<h1>
<p @click="handleOne">子路由1</p>
<p @click="handleTwo">子路由2</p>
</h1>
<router-view></router-view>
</div> </template>
<script>
export default {
methods: {
handleOne() {
this.$router.push('/routerChildren/one')
},
handleTwo() {
this.$router.push('/routerChildren/two')
}
}
}
</script>
<style scoped>
h1 {
display: flex;
justify-content: center;
}
p {
margin: 20px;
cursor: pointer;
}
</style>

在这里我们一定要注意需要在这个组件中带上 <router-view></router-view>这个标签
否则,你切换的one组件和two组件里面的内容不会显示,
谈下个人对<router-view/>这玩意儿的理解:
我感觉这个东西就是一个当前组件的容器,与当前组件相关联的组件想要显示,就需要通过他,也就是说一层路径对应一个<router-view/>,每一个与组件相匹配的路由都会在<router-view/>中显示
我们的项目中所有组件都关联在app这个组件上,如何进行切换显示的呢,就是通过这个东西<router-view/>,
所以说如果app下面有个One组件,但是在One组件里想要通过跳转来显示OneOne及OneTwo组件,你就必须在One组件里写一个<router-view/>。
ok,结束!!
vue 路由嵌套 及 router-view vue-router --》children的更多相关文章
- Vue路由嵌套
Vue路由嵌套 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- vue路由中 Navigating to current location ("/router") is not allowed
报错原因:多次点击同一路由,导致路由被多次添加 解决方法: router/index中添加以下代码: //router/index.js Vue.use(VueRouter) //导入vue路由 co ...
- vue教程3-06 vue路由嵌套(多层路由),路由其他信息
多层嵌套: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- vue 路由嵌套高亮问题
正常路由嵌套是没有问题的,但是如果你已经在当前主路由页面了,然后再次点击主路由就会出现页面数据空白的情况 看代码: //主路由通过v-for循环出来 <div class="list- ...
- Vue路由嵌套和命名视图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue路由--嵌套路由
静态嵌套路由: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- 关于vue路由嵌套遇到的坑~
关键在于子路由中的path问题,path之前不要放/ <!DOCTYPE html> <html lang="en"> <head> <m ...
- vue路由嵌套,vue動態路由
https://www.cnblogs.com/null11/p/7486735.html https://www.cnblogs.com/goloving/p/9271501.html https: ...
- vue路由嵌套,对应展示的视图
随机推荐
- 如何编写testbench的总结(非常实用的总结)
1.激励的设置 相应于被测试模块的输入激励设置为reg型,输出相应设置为wire类型,双向端口inout在测试中需要进行处理. 方法1:为双向端口设置中间变量inout_reg作为该inout的输出寄 ...
- 高并发-原子性-AtomicInteger
线程不安全: //请求总次数private static int totalCount = 10000;//最大并发数private static int totalCurrency = 100;// ...
- CentOS8 安装部署Apache+Php+MariaDB(pdo扩展)
使用新的CentOS8系统架设PHP服务器,因现在主流数据库mysql已闭源了,所以现在改为使用MariaDB.而php7以后不支持mysqli链接,只有pdo方式,为了安装pdo扩展,所以重新编译安 ...
- sql视频学习关键笔记(自用记单词与学习用)
sql字段类型 numeric(18,3)-18位整数加3位小数点(货币计量最好选这类型) sql关键字 insert. update. delete alter grant 授权. revoke 回 ...
- Spring AOP expose-proxy
写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...
- 认识.net Framework
- mysql UNION操作符 语法
mysql UNION操作符 语法 作用:用于合并两个或多个 SELECT 语句的结果集. 语法:SELECT column_name(s) FROM table_name1 UNION SELECT ...
- POJ 1252 Euro Efficiency ( 完全背包变形 && 物品重量为负 )
题意 : 给出 6 枚硬币的面值,然后要求求出对于 1~100 要用所给硬币凑出这 100 个面值且要求所用的硬币数都是最少的,问你最后使用硬币的平均个数以及对于单个面值所用硬币的最大数. 分析 : ...
- Java——package与import
[package] <1>为了解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间. <2>package作为源文件的第一条语句(缺省时指定为 ...
- UVALive 6855 Banks (暴力)
Banks 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/A Description http://7xjob4.com1.z0 ...