vue-router编程式导航

在vue项目中经常用到this.$router.push() 和 this.$router.replace() 方法进行路由跳转就是编程式导航。。。

通俗理解编程式导航:通过操作$router实例的JavaScript代码实现路由跳转。点击 <router-link :to="..."> 等同于调用 router.push(...)。$router实例的方法有push()、replace()、go()方法,也可以进行路由传参。

先上代码简单入手看效果:

demo基于vue-cli创建的,项目结构如图:

第一步:定义路由文件 router >index.js如下

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'import home from '@/components/Home'   //引入组件Vue.use(Router)export default new Router({routes: [{path: '/',name: 'HelloWorld',component: HelloWorld},{path: '/home',       //可以定义路由路径、命名、组件、参数、别名、重定向name: 'home',component: home}, ]
})

第二步:helloWorld页面使用声明式和编程式导航到home组件的按钮,helloWorld组件如下

<template><div class="hello"><h1>{{ msg }} 页面</h1><router-link to="/home">声明式使用router-link路由导航到home页面</router-link><br><button @click="pushToHome()">js编程式push方法导航到home页面</button><button @click="replaceToHome()">js编程式replace方法导航到home页面</button><br><button @click="goNext()">js编程式go方法导航到history记录的下一个页面</button><router-view></router-view></div>
</template><script>
export default {name: 'HelloWorld',data () {return {msg: 'Welcome to HelloWorld' }},methods:{pushToHome:function(){this.$router.push("/home",function(){   //push()方法的使用,包括导航成功或失败的回调函数console.log("导航成功的回调函数");},function(){console.log("导航中止的回调函数");}); },replaceToHome:function(){               //replace()方法的使用this.$router.replace("/home"); },goNext:function(){                  //go()方法的使用this.$router.go(1); }}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
button {margin:10px;
}
</style>

第三步骤页面:home组件中可以使用router.go(-1) 返回到上一个历史记录中的页面;无记录时点击无效。home组件如下

<template><div class="hello"><h1>{{ msg }}</h1><button @click="goBack()">js编程式go方法返回history页面</button></div>
</template><script>
export default {name: 'Home',data () {return {msg: 'Welcome to Home 主页!!!!' }},methods:{goBack:function(){this.$router.go(-1);   }}
}
</script><style scoped>
h1, h2 {color:red;
}
</style>

router.push(location, onComplete, onAbort)方法

1、router.push() 方法,参数分别指的是导航到 新的url、导肮完成的回调函数、导航中止的回调函数 

2、router.push() 方法 会向history栈添加一个新的记录 ,所以当用户点击浏览器后退按钮时,则回到之前的 URL。

3、router.push(location)方法使用

//使用路由的path值
this.$router.push("/router1");//使用包含路由path键值的对象
this.$router.push({path:'/router1'});//使用包含路由name键值的对象
this.$router.push({name:'routerNum1'});//使用包含name键值和params参数对象的对象,
//注意:该方式params传参浏览器页面不在url中显示参数,但是会页面刷新的时候丢失参数
this.$router.push({name:'routerNum1',params:{userId:123}}); //path键值结合params的形式传参无效
this.$router.push({path:'/router1',params:{userId:123}});//可以使用path键值结合query的形式传参
this.$router.push({path:'/router1',query:{userId:123}});//使用path值拼接参数来传参,注意这里使用的不是单引号!!!
const userId=123;
this.$router.push({path:`/router1/${userId}`}); 

4、 push()编程式导航注意点:

const userId = 1231、router.push({ path: `/router1/${userId}` }) // 注意使用反引号2、router.push({ path: '/router1', params: { userId }}) // 注意path键值结合params传参无效3、router.push({ name: '/routerNum1', params: { userId }}) // 注意该方式在浏览器url不显示参数,但是页面刷新会导致参数丢失

4、小demo使用验证如下:

第一步:定义路由文件 router >index.js如下

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'import home from '@/components/Home'
import router1 from '@/components/Router1'
import router2 from '@/components/Router2'Vue.use(Router)export default new Router({routes: [{path: '/',name: 'HelloWorld',component: HelloWorld},{path: '/home',  name: 'home',component: home},{path: '/router1',name: 'routerNum1',component: router1},{path: '/router2',name: 'routerNum2',component: router2},    ]
})

第二步:home.vue组件文件代码如下。

<template><div class="hello"><h3>{{ msg }}</h3><button @click="goBack()">js编程式go方法返回history页面</button><hr><h3>js编程式导航中push方法的使用:</h3><button @click="pathTo()">push方法的url参数使用path值导航</button><button @click="pathObjectTo()">push方法的url参数使用path对象导航</button><br><button @click="nameObjectTo()">push方法,包含路由命名name键值的对象导航</button><br><button @click="nameParamTo()">push方法,包含路由命名name键值的对象结合params传参</button><br><button @click="pathParamTo()">push方法,path键值对象导航结合params传参,参数传递失败</button><button @click="pathQueryTo()">push方法,path键值对象导航结合query传参</button><br><button @click="pathandTo()">push方法,path值拼接参数来传参</button></div>
</template><script>
export default {name: 'Home',data () {return {msg: 'Welcome to Home 主页!!!!' }},methods:{goBack:function(){this.$router.go(-1);   },pathTo:function(){     //使用路由的path值this.$router.push("/router1");},pathObjectTo:function(){   //使用包含路由path键值的对象this.$router.push({path:'/router1'});},nameObjectTo:function(){  //使用包含路由name键值的对象this.$router.push({name:'routerNum1'});},nameParamTo:function(){  //使用包含name键值和params参数对象的对象this.$router.push({name:'routerNum1',params:{userId:123}}); },pathParamTo:function(){  //path键值结合params的形式传参无效this.$router.push({path:'/router1',params:{userId:123}}); },pathQueryTo:function(){  //可以使用path键值结合query的形式传参this.$router.push({path:'/router1',query:{userId:123}}); },pathandTo:function(){const userId=123;this.$router.push({path:`/router1/${userId}`});  }}
}
</script><style scoped>
h1, h2 {color:red;
}
</style>

第三步:router1.vue组件代码如下,使用this.$route.params.paramName或query.queryName接收路由传的参数

<template><div class="hello"><h3>{{ msg }}</h3><button @click="goBack()">js编程式go方法上一个路由页面</button><p>{{fromRouterParams}}</p><p>{{fromRouterQuery}}</p></div>
</template><script>
export default {name: 'Router1',data () {return {msg: 'Welcome to Router1' ,fromRouterParams:this.$route.params.userId,fromRouterQuery:this.$route.query.userId,}},methods:{goBack:function(){this.$router.go(-1);        },}
}
</script><style scoped>
h1, h2 {color:red;
}
</style>

router.replace(location, onComplete, onAbort)方法

跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录;

router.go(n)方法

1、参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。 Browser History APIs点击了解更多

2、使用:

// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)// 后退一步记录,等同于 history.back()
router.go(-1)// 前进 3 步记录
router.go(3)// 如果 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)

Vue Router 的导航方法 (push、 replace、 go) 在各类路由模式 (history、 hash 和 abstract) 下表现一致。

路由重定向:

路由配置{ path: '/a', redirect: '/b' },路由重定向是指当用户访问 /a时,路由跳转到b路由,URL 将会被替换成 /b

1、在路由配置文件 router >index.js上使用:

const router = new VueRouter({routes: [{ path: '/router1', redirect: '/router2' },  //是从 /route1 重定向到 /router2{ path: '/b', redirect: {name:'foo'} },  //重定向的目标也可以是一个命名的路由{ path: '/c', redirect:(to)=>{// 方法接收 目标路由 作为参数// return 重定向的 字符串路径/路径对象//注意:导航守卫应用在导航跳转目标to上}},]
})

2、注意在给路由设置有路由重定向时,再使用的路由导航会应用在跳转to指向的路由上,如下路由router1上的导航守卫无效

    {path: '/router1',redirect: '/router2',name: 'routerNum1',component: router1,beforeEnter:(from,to,next)=>{       //导航守卫无效console.log("在路由1上进行重定向");   next();}},{path: '/router2',name: 'routerNum2',component: router2,beforeEnter:(from,to,next)=>{console.log("进入到了路由2");next();}},    

3、重定向指向自身会报错:[Vue warn]: Error in beforeCreate hook: "RangeError: Maximum call stack size exceeded,使用动态路径参数传参除外

路由别名

路由别名可以理解为路由的 '小名',可以使用路由别名进行路由导航

路由 /router2 的别名 alias 为 /secondRouter ,那么使用别名访问 /secondRouter 时,url是/secondRouter,导航跳转到路由 /router2 ,如下

    {path: '/router2', name: 'routerNum2', alias:'/secondRouter',component: router2 },

动态路由匹配:

使用情况:

1、如果导航目的地和当前路由相同,只有参数发生了改变 (比如从一个用户资料到另一个 /users/1 -> /users/2),需要组件复用,组件复用意味着组件的生命周期钩子函数不会被调用

2、或者所有路由映射到同一个组件模式渲染,只是参数不同时。那么,我们可以在 vue-router 的路由路径中使用“动态路径参数”(dynamic segment) 来达到这个效果:

动态路径参数的使用

第一步:动态路径参数的使用需要在路由配置文件上使用冒号开头的形式

    {//动态路径参数的使用 冒号开头//那么像/router1/1 和/router1/2都将跳转到该路由页面path: '/router1/:userNum',   name: 'routerNum1',component: router1,},

第二步:使用this.$router.push({path:  `/router1/${userNum1}` }); 形式进行动态路径传参,注意反引号,url中的参数不会刷新丢失

<template><div class="hello"><h3>{{ msg }}</h3><button @click="goBack()">js编程式go方法返回history页面</button><hr><h3>js动态路由匹配的使用:</h3><button @click="pathParam1To()">动态路径参数1导航</button><button @click="pathParam2To()">动态路径参数2导航</button><hr><button @click="diffParamsTo()">导航到当前组件(组件复用),但传递的参数不同</button></div>
</template><script>
export default {name: 'Home',data () {return {msg: 'Welcome to Home 主页!!!!' }},watch: {'$route' (to, from) {// 对路由变化作出响应...console.log('路由发生变化');console.log(this.$route.params.userNum);}},methods:{goBack:function(){this.$router.go(-1);   },pathParam1To:function(){let userNum1=111111;this.$router.push({path:`/router1/${userNum1}`}); },pathParam2To:function(){let userNum2=222222;this.$router.push({path:`/router1/${userNum2}`}); },diffParamsTo:function(){let userNum2=666;this.$router.push({path:`/router1/${userNum2}`}); },}
}
</script><style scoped>
h1, h2 {color:red;
}
</style>

第三步:可以在路由组件中使用this.$route.params获取动态路径参数,如下

<p>{{this.$route.params.userNum}}</p>

组件复用

复用组件时,想对路由参数的变化作出响应的话,你可以简单地 watch (监测变化) $route 对象:

  watch: {'$route' (to, from) {// 对路由变化作出响应...console.log('路由发生变化');console.log(this.$route.params.userNum);}},

也可以使用2.2中引入的 beforeRouteUpdate导航守卫来响应这个变化 (比如抓取用户信息)。组件内导航守卫可以点击了解

const User = {template: '...',beforeRouteUpdate (to, from, next) {// react to route changes...// don't forget to call next()}
}

嵌套路由

嵌套路由指的是在渲染的路由组件页面中包含了下级路由渲染出口(<router-view>),浏览器的URL 中各段动态路径也按某种结构对应嵌套的各层组件,

在项目的app.vue 文件中<router-view>是最顶层的出口,渲染最高级路由匹配到的组件。

<div id="app"><router-view></router-view>
</div>

第一步:定义路由配置文件router >index.js,在路由上使用children属性定义下级路由

import Vue from 'vue'
import Router from 'vue-router'
import helloWorld from '@/components/HelloWorld'
import home from '@/components/Home'
import book from '@/components/Book'
import water from '@/components/Water'Vue.use(Router)
export default new Router({routes: [{path: '/',component:helloWorld},{path: '/home',component: home,children:[{path:'',     //当访问的子路由路径无匹配时会指向到该空的子路由component:water,},{path:'book',component:book,},{path:'water',component:water,},]},]
})

第二步:路由组件home.vue的内容,子路由导航包括了声明式和编程式导航

<template><div class="hello"><h3>{{msg}}</h3><button @click="showBookRouter()">点击按钮显示子路由页面book</button><button @click="showWaterRouter()">点击按钮显示子路由页面water</button><br><router-link to="/home/book">book</router-link><router-link to="/home/water">water</router-link><router-link to="/home/">foo</router-link><router-view></router-view></div>
</template><script>
export default {name: 'Home',data () {return {msg: 'Welcome to Home !!!'}},methods:{showBookRouter:function(){this.$router.push('/home/book');  //编程式路由导航},showWaterRouter:function(){this.$router.push('/home/water');},}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {color:red;
}
</style>

第三步:子组件book.vue 和water.book这里就不写了

命名视图

同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar (侧导航) 和 main (主内容) 两个视图,这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。如果 router-view 没有设置名字,那么默认为 default

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>

一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件。确保正确使用 components配置 (带上 s):

    {path: '/',components: {default: water,a: book,b: water}}

参考网址:官网API https://router.vuejs.org/zh/guide/essentials/navigation.html

vue-router详尽:编程式导航、路由重定向、动态路由匹配、路由别名、嵌套路由、命名视图相关推荐

  1. Vue页面与页面之间的传值(router.push()编程式导航)

    页面与页面之间跳转传值,其实我们一般用两种,query传参或者用params 其实这两个方式都是页面导航的一种,编程式导航:通过调用JavaScript形式的API实现导航的方式,大家不了解的可以去官 ...

  2. 031_vue编程式导航

    1. 页面导航的两种方式 1.1. 声明式导航: 通过点击链接实现导航的方式, 叫做声明式导航.例如: 普通网页中的<a></a>链接或vue中的<router-link ...

  3. Vue.js-Day05【安装路由(vue-router)、如何使用vue-router、404配置、激活class、动态路由、编程式导航、路由嵌套、路由元信息、导航拦截】

    Vue.js实训[基础理论(5天)+项目实战(5天)]博客汇总表[详细笔记] 目   录 1.单页面应用 1.1.多页面应用 1.2.单页面应用 1.3.vue-router 2.安装vue-rout ...

  4. vue编程式导航,命名路由

    //使用 window.location.href 的形式,叫做 编程式导航 绑定事件的形式 <template><div class="goods-item" ...

  5. vue编程式导航跳转到当前路由多次执行抛出NavigationDuplicated: Avoided redundant navigation to current location:

    为什么? push方法会返回一个Promise对象,内部会进行跳转成功或失败的回调 解决方法 只需在push方法末尾传递两个成功和失败的回调函数,可以捕获到当前的错误,可以解决.但是此种方法治标不治本 ...

  6. 页面导航的两种方式——声明式导航、编程式导航||vue-router编程式导航||router.push() 方法的参数规则

    页面导航的两种方式 vue-router编程式导航 编程式导航基本用法 <!DOCTYPE html> <html lang="en"><head&g ...

  7. 前端笔记(11) Vue3 Router 编程式导航 router.push router.replace

    什么是编程式导航? 在上篇博客Vue3 Router 监听路由参数变化中,我们使用 <router-link> 创建 a 标签来定义导航链接: <router-link to=&qu ...

  8. Vue3.x动态路由、命名路由和编程式导航

    1.动态路由的定义和使用 两种方法 :         1.路由中的动态传参以 : 进行声明,冒号后面跟的是动态参数名(path: 'apple/:mid'),通过{{ $route.params.m ...

  9. vue2项目复习01-关闭elint检校,src文件别名,路由传参的对象写法,代理解决跨域问题,nprogress,vuex状态管理库,store的模块式开发,节流与防抖,编程式导航+事件委托路由跳转

    1.关闭elint语法校验 创建vue.config.js //关闭elint语法校验 {lintOnSave:false; } 2.src文件夹配置别名 jsconfig.json配置别名 @代表s ...

最新文章

  1. SpringCloud配置文件 application.yml和 bootstrap.yml区别,执行顺序
  2. Idea(二) 解决IDEA卡顿问题及相关基本配置(转)
  3. mina应用程序架构(翻译)
  4. 部分网站公开数据的汇总(2)
  5. mysql子查询sysdate_Oracle向MySQL 迁移相关事项
  6. 推荐几个电子/嵌入式方向的公众号
  7. 高效的企业测试-单元和用例测试(2/6)
  8. linux 取消混杂模式,Linux下网卡混杂模式设置和取消
  9. linux与windows查看占用端口的进程ID并杀死进程
  10. 速达软件开发版使用技巧-每页固定行样式报表设计
  11. 取出被正则表达式匹配的值
  12. pandas 日期比较大小_如何计算不同pandas列中两个日期之间的年数
  13. c语言试题 函数选择,(C语言函数章节选择题.doc
  14. PaddlePadlle2.3实现人脸关键点检测
  15. 从单体应用到微服务开发旅程
  16. 基于SpringBoot点餐小程序的开发【前后端】
  17. 什么是嵌入式培养?要不要选嵌入式培养?
  18. MSL(湿气等级)2020-09-29
  19. 【Hive】大数据的排序和拼接
  20. java国漫论坛网站计算机毕业设计MyBatis+系统+LW文档+源码+调试部署

热门文章

  1. cad怎么向下位移_CAD制图初学入门教程:阵列功能的使用技巧
  2. sql count为空时显示0_C0010负坐标显示为正数+红色0值参考线
  3. hive安装mysql驱动_Hadoop-2.6.0为基础的Hive安装
  4. java 数据保存内存_java中的各种数据类型在内存中存储的方式 一
  5. mysql双主数据一致性_MySQL双主一致性架构优化 | 架构师之路-阿里云开发者社区...
  6. Java Hashtable keySet()方法与示例
  7. python多线程框架_基于python和bash的多线程任务框架 不要让cpu闲着了
  8. PHP array_pad()函数与示例
  9. getsimplename_Java类类getSimpleName()方法的示例
  10. java jar包示例_Java包getSpecificationVersion()方法和示例