1 # 一、路由的props参数
2 export default new VueRouter({
3 routes:[
4 {
5 name:'guanyu', // 命名路由
6 path:'/about', // 路劲
7 component: About
8 },
9 {
10 path:'/home',
11 component: Home,
12 children:[
13 {
14 path:'messages',
15 component: Messages,
16 children:[
17 {
18 name: 'xiangqing',
19 path: 'message/:id/:title', // params传参
20 component: MessageInfo,
21 // props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传值给MessageInfo组件。
22 // props: {a:1,b:'hello'},
23
24 // props的第二种写法,值为bool值,为true,就会把该路由组件收到的参数,以props的形式传值给MessageInfo组件。
25 props: true,
26
27 // props的第三种写法,值为函数
28 // props($route){
29 // return {
30 // id: $route.query.id,
31 // title: $route.query.title,
32 // a: 1,
33 // b: 'hello'
34 // }
35 // }
36 }
37 ]
38 },
39 {
40 path:'news',
41 component: News
42 }
43 ]
44 }
45 ]
46 });
47
48 # 二、<router-link>的replace属性
49 # 1.作用:控制路由跳转时操作浏览器历史记录的前进后退模式
50 # 2.浏览器的历史记录有两种写入方式:分别为push和replace,push是追加历史记录,replace是替换当前记录,路由跳转默认是push。
51 # 3.如何开启replace模式:
52 <router-link replace ...>...</router-link>
53
54 # 三、编程式路由导航
55 1.作用:不借助<router-link>实现路由跳转,让路由跳转更灵活
56 2.具体编码:
57 this.$router.push({ // 留痕迹在window.history
58 name: 'xxx',
59 params: {id:xxx,title:'xxx'}
60 });
61 this.$router.replace({// 不留痕迹在window.history
62 name: 'xxx',
63 params: {id:xxx,title:'xxx'}
64 });
65 this.$router.forward(); // 前进,等于window.history.forward()
66 this.$router.back(); // 后退,等于window.history.back()
67 this.$router.go(-2); // 可前进可后退,等于window.history.back()
68
69 # 四、缓存路由组件
70 1.作用:让不展示的路由组件保持挂载,不被销毁
71 2.具体编码:<keep-alive include="News,Meaasges"><router-view></router-view></keep-alive>
72 注意:被<keep-alive>包含的内容会保持挂载不被销毁,include可以指定哪些组件不被销毁,它的值是组件名(也就是组件的name属性值),不填include代表所有都不销毁
73
74 # 五、两个新的声明周期钩子:activated、deactivated
75 1.作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态
76 2.具体名字:activated路由组件被激活时触发,deactivated路由组件失去激活时触发
77
78 # 六、全局守卫路由
79 # 1.全局前置路由守卫——初始化的时候被调用、每次路由切换之前被调用
80 router.beforeEach((to, from, next)=>{
81 console.log('beforeEach', to, from);
82 // if (to.name === 'xinwen' || to.path === '/home/messages'){ // 这里要是有十几二十个的话,就会显得累赘
83 if (to.meta.isAuth){ // 路由的meta属性对象,是专门供我们自定义属性使用的(注意:自定义属性放在其他地方不会生效)
84 if (localStorage.getItem('school')==='BaiYeShu'){
85 next();// 放行
86 console.log('@');
87 }else{
88 alert('学校名不对!')
89 }
90 }else{
91 next();
92 }
93 });
94 # 2.全局后置路由守卫——初始化的时候被调用、每次路由切换之后被调用
95 router.afterEach((to, from)=>{
96 console.log('afterEach', to, from);
97 if (to.meta.title){
98 document.title = to.meta.title
99 }else{
100 document.title = 'vue_test'
101 }
102
103 });
104 export default router;
105 # 3.独享路由(配置在路由内部)
106 {
107 path:'news',
108 component: News,
109 meta:{isAuth: true, title: '新闻'}, // 路由的meta属性对象
110 beforeEnter(to, from, next){ // 这就是独享路由,内容参数和beforeEach没区别
111 console.log('beforeEnter', to, from);
112 // if (to.name === 'xinwen' || to.path === '/home/messages'){ // 这里要是有十几二十个的话,就会显得累赘
113 if (to.meta.isAuth){
114 if (localStorage.getItem('school')==='BaiYeShu'){
115 next();// 放行
116 console.log('@');
117 }else{
118 alert('学校名不对!')
119 }
120 }else{
121 next();
122 }
123 }
124 }
125 # 4.组件内部路由守卫(写在组件内部的回调)
126 // 通过路由规则,进入该组件时被调用
127 beforeRouteEnter(to, from, next) {
128 console.log('beforeRouteEnter');
129 next();// 放行
130 },
131 // 通过路由规则,离开组件时被调用
132 afterRouterLeave(to, from, next) {
133 console.log('afterRouterLeave');
134 next();// 放行
135 }
136

Vue 路由的一些复杂配置的更多相关文章

  1. vue 路由(二级)配置及详细步骤

    1.安装插件(可以选择在初始化项目的时候安装) cnpm install vue-router --save-dev 2.将插件全局引入到项目中(main.js) import VueRouter f ...

  2. vue路由history模式,nginx配置

    nginx配置内容 # For more information on configuration, see: # * Official English Documentation: http://n ...

  3. Vue 路由配置、动态路由

    1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...

  4. vue路由配置

    1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...

  5. Vue 路由模块化配置

    博客地址:https://ainyi.com/77 企业运营后台页面很多,路由如若不区分模块化配置,所有路由挤在同一个文件将不好维护,所以路由的配置也要模块化 分享两个解决方案 -- Vue 路由配置 ...

  6. vue路由的两种模式配置以及history模式下面后端如何配置

    vue路由的两种模式配置以及history模式下面后端如何配置 1.hash ——即地址栏URL中的#符号.hash 虽然出现URL中,但不会被包含在HTTP请求中,对后端完全没有影响,因此改变has ...

  7. 简单配置Vue路由

    简单配置Vue路由 1.  创建一个单文件组件Test.vue <template> <div>Test</div> </template> <s ...

  8. Vue之创建组件之配置路由!

    Vue之创建组件之配置路由!== 第一步: 当然就是在我们的试图文件夹[views]新建一个文件夹比如home 在home文件夹下面新建一个文件index.vue 第二步:在router目录下做如下事 ...

  9. npm vue路由配置

    npm vue路由 复习:1.README.md文件:保存如何使用的命令 (1)     npm install:拷项目时可以不拷node_modules文件,运行该命令时,会自动下载node_mod ...

随机推荐

  1. mysql忘记root密码实现免密登录

    1.配置my.cnf文件,跳过授权表: skip-grant-tables 2.重启mysqld服务 3.z直接mysql登录 4.use mysql这个数据库 5.设置密码: update user ...

  2. ML第7周学习小结

    本周收获 总结一下本周学习内容: 1.学习了<深入浅出Pandas>的第六章:Pandas分组聚合 6.4 聚合统计 6.5 数据分箱 6.6 分组可视化 博客: pandas:聚合统计. ...

  3. Spring-boot整合Activiti7

    Spring-boot整合Activiti7 pom.xml    <properties>        <maven.compiler.source>15</mave ...

  4. 记一次 JDK SPI 配置不生效的问题 → 这么简单都不会,还是回家养猪吧

    开心一刻 今天去幼儿园接小侄女,路上聊起了天 小侄女:小叔,今天我吃东西被老师发现了 我:老师说了什么 小侄女:她说拿出来,跟小朋友一起分享 我:那你拿出来了吗 小侄女一脸可怜的看向我,说道:没有,我 ...

  5. Win10 LTSC 2021 安装及相关bug解决

    Win10 LTSC 2021 安装及相关bug解决 目录 Win10 LTSC 2021 安装及相关bug解决 准备文件 系统安装 系统激活 修复CPU占用高和输入法显示bug 安装微软应用商店 推 ...

  6. 【原创】快速理解Unicode和utf-8的本质

    字符串编码 基本概念 在代码中处理,为了字节统一,都统一使用Unicode 核心:在pyhton中s.encode("utf-8")中的变量实例s必须是已经是Unicode格式,否 ...

  7. LVS+keepalived高可用

    1.keeplived相关 1.1工作原理 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题. 在一个LVS服务集群中通常有主服务器(MAS ...

  8. 测试软件稳定性、健壮性之Monkey工具--简洁与深入

    搭建环境这章节没做详细说明,因为我是前期做APP自动化是已经将 SDK 以及JDK给安装配置好了,这次是直接上来演示monkey的功能点以及运用 一.什么是稳定性测试? 通过随机点击屏幕一段时间,看看 ...

  9. 000 上传本地库到Github远程库过程全记录

    20220613 Github上新创建了一个CsImage库,之后本地创建了一个对应名称的目录,并创建本地库,进行了上传操作,记录一下过程 1.Github上CsImage库创建完成 Github上创 ...

  10. vue根据后端菜单自动生成路由(动态路由)

    vue根据后端菜单自动生成路由(动态路由) router.js import Vue from 'vue' import Router from 'vue-router' import store f ...