当前位置:网站首页>编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
2022-07-26 12:18:00 【呵呵哒( ̄▽ ̄)'】
>>重写push和replace方法
1.编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
--路由跳转有两种形式:声明式导航、编程式导航
--声明式导航没有这类问题,因为vue-router底层已经处理好了。
1.1 为什么编程式导航进行路由跳转的时候,就有这种警告错误?
"vue-router":"^3.5.3":最新的vue-router引入promise
1.2 通过给push方法传递相应的成功、失败的回调函数,可以捕获到当前错误,可以解决。
1.3 通过底部的代码,可以实现解决错误
this.$router.push({
name:'search',
params:{
keyword:this.keyword
},
query:{
k:this.keyword.toUpperCase()
}
},()=>{
},(error)=>{
console.log(error)
})
这种写法:治标不治本,将来在别的组件当中push|replace,编程式导航还是有类似的错误
1.4
返回promise。为什么会出现如上的警告?因为promise需要传递一个成功或者失败的回调,这样才能处理掉这些异常。
this:当前组件实例(search)
this.$router属性:当前的这个属性,属性值是VueRouter类的一个实例,当在入口文件注册路由的时候,给组件实例添加$router|$route属性
this关键字,也就是所谓的执行上下文
push:VueRouter类的一个实例
function VueRouter(){
}
VueRouter.prototype.push = function(){
//函数的上下文为VueRouter类的一个实例
}
//原型对象的方法
let $router = new VueRouter()
$router.push(xxx)
this.$router.push();(1)先把VueRouter原型对象的push,先保存一份 (2)重写router.prototype上的push方法 (3)若外部传入了resolve和rejected回调,那么就用外部传入的;否则就传入一个空函数作为回 调,这样就可以避免没有传入回调时的报错。 (4)使用call的原因:因为originPush已经被抽取为一个全局函数了,若push被调用的时候,不显式指定this,则this会指向window,而不是VueComponent实例。
//先把VueRouter原型对象的push,先保存一份
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
//重写push|replace
//第一个参数:告诉原来push方法,你往哪里跳转(传递哪些参数)
//第二个参数:成功回调
//第三个参数:失败的回调
//call||apply区别
//相同点,都可以调用函数一次,都可以篡改函数的上下文一次
//不同点:call与apply传递参数;call传递参数用逗号隔开,apply方法执行,传递数组
VueRouter.prototype.push = function (location,resolve,reject){
if(resolve && reject){
originPush.call(this,location,resolve,reject)
}else {
originPush.call(this,location,()=>{},()=>{})
}
}
VueRouter.prototype.replace = function (location,resolve,reject){
if(resolve && reject){
originReplace.call(this,location,resolve,reject)
}else{
originReplace.call(this,location,()=>{},()=>{})
}
}>>call和apply区别
- 相同点,都可以调用函数一次,都可以篡改函数的上下文一次
- 不同点:call与apply传递参数;call传递参数用逗号隔开,apply方法执行,传递数组
边栏推荐
- Pytest interface automated testing framework | introduction to fixture of pytest
- QT入门引导 及其 案例讲解
- 三维点云课程(八)——特征点匹配
- y9000p2022重装win10问题
- Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
- How RFID works
- 【2243】module_param.m
- Minesweeping games - easy to play addictive (C language version)
- Oracle AWR report script: SQL ordered by elapsed time
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
猜你喜欢

行业案例|指标中台如何助力银行业普惠金融可持续发展

FPGA入门学习(一) - 第一个FPGA工程

Use and optimization of MySQL composite index (multi column index)

Network protocol: tcp/ip protocol

字节流习题遇到的问题及解决方法

Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging

动静态库的实现(打包动静态库供他人使用)

连锁店收银系统如何帮助鞋店管理好分店?

Map函数统计字符出现的次数

2022 年要了解的新兴安全供应商
随机推荐
You Yuxi recommends vite to beginners [why use vite]
行业案例|指标中台如何助力银行业普惠金融可持续发展
自定义浏览器默认右击菜单栏
【微信小程序】一文读懂,数据请求
Oracle的Windows版本能在linux中使用吗?
14.2 byte stream learning
Flutter's learning path
微软关闭了两种攻击途径:Office 宏、RDP 暴力破解
Can the Windows version of Oracle be used in Linux?
面试官:如何处理高并发?
X.509、PKCS文件格式介绍
Redis主从复制原理
Pytest interface automated testing framework | using multiple fixtures
Detailed explanation of Legendre transformation and conjugate function
什么是OOM,为什么会OOM及一些解决方法
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
Redis master-slave replication principle
Some common writing methods and skills
Pytest interface automation test framework | rerun failed cases
羽毛球馆的两个基础设施你了解多少?
