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的更多相关文章

  1. Vue路由嵌套

    Vue路由嵌套 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. vue路由中 Navigating to current location ("/router") is not allowed

    报错原因:多次点击同一路由,导致路由被多次添加 解决方法: router/index中添加以下代码: //router/index.js Vue.use(VueRouter) //导入vue路由 co ...

  3. vue教程3-06 vue路由嵌套(多层路由),路由其他信息

    多层嵌套: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. vue 路由嵌套高亮问题

    正常路由嵌套是没有问题的,但是如果你已经在当前主路由页面了,然后再次点击主路由就会出现页面数据空白的情况 看代码: //主路由通过v-for循环出来 <div class="list- ...

  5. Vue路由嵌套和命名视图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vue路由--嵌套路由

    静态嵌套路由: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. 关于vue路由嵌套遇到的坑~

    关键在于子路由中的path问题,path之前不要放/ <!DOCTYPE html> <html lang="en"> <head> <m ...

  8. vue路由嵌套,vue動態路由

    https://www.cnblogs.com/null11/p/7486735.html https://www.cnblogs.com/goloving/p/9271501.html https: ...

  9. vue路由嵌套,对应展示的视图

随机推荐

  1. windows使用cmd命令输出文件清单和文件树

    输出目录树:tree /f > d:\filetree.txt 输出目录清单:dir /s /b > d:\filelist.txt

  2. rev 反向输出文件内容

    1.命令功能 rev 按行反向输出文件内容 2.语法格式 rev  file 3.使用范例 [root@localhost ~]# echo {a..k} >> test [root@lo ...

  3. The Complex Inversion Formula. Bromwich contour.

    网址:http://www.solitaryroad.com/c916.html

  4. QKeyEvent

    #include<QKeyEvent>键盘事件由两个重载函数,实现即可:  void keyReleaseEvent(QKeyEvent *event);  void keyPressEv ...

  5. dd命令注意:dd:unrecognized operand 'if'

    如果是 idd if=boot.bin 在等号两边不要有空格

  6. 正确重写hashCode方法

    https://blog.csdn.net/HD243608836/article/details/87367763 到这里,对象写完了,开始描述问题. 计算hashCode的注意事项: 1.不能包含 ...

  7. Python pdfkit

    序言 住在地下室的人,依然有仰望星空的权利. pdfkit python使用pdfkit中,如果使用pdfkit.fromurl 或者pdfkit.fromstring等,就会出现上述错误.而且如果你 ...

  8. Codeforces Round #351(Div 2)

    熬到半夜打结果前一个小时提交代码的页面根本加载不进去,D题写完还因为小点没判被hack掉了... rating什么的都是身外之物了,于是乎喜闻乐见地又掉回绿名了. A题: *题目描述: Bear看一场 ...

  9. 【PowerOJ1736&网络流24题】飞行员配对方案问题(最小割)

    题意: n<=100,要求输出方案 思路:准备把没刷的24题从头搞一遍 输出方案的话就在增广的时候记一下另一端的编号就好 #include<bits/stdc++.h> using ...

  10. es之词库热更新解决方案

    1. 下载tomcat,作为远程词库的容器 , 需要在tomcat中配置词库 /webapp/ROOT这个路径下新建一个远程词库:​Vim hot.dicHot.dic中存放的就是实时热词 2.测试t ...