当前位置:网站首页>编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出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方法执行,传递数组
边栏推荐
- JVM内存溢出和内存泄漏的区别
- Use and optimization of MySQL composite index (multi column index)
- File类的学习过程中出现的问题及解决方法
- Pytest interface automation test framework | execute use cases through markup expressions
- El form displays two columns per row, with the bottom button centered
- Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
- FPGA入门学习(三)- 38译码器
- 结合环境光、接近传感以及红外测距的光距感芯片4530A
- 腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
- Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
猜你喜欢

How do children's playgrounds operate?

使用fastJson中的JSONObject对象简化POST请求传参

向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
![[Anhui University] information sharing of postgraduate entrance examination and re examination](/img/71/258b6b740d2c0e12d77e30f2df8a6e.jpg)
[Anhui University] information sharing of postgraduate entrance examination and re examination

2、 Container_

The map function counts the number of occurrences of characters

Understand the string class

Redis master-slave replication principle

.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI

结合环境光、接近传感以及红外测距的光距感芯片4530A
随机推荐
面试官:如何理解QPS,TPS,RT?
Introduction to FPGA (I) - the first FPGA project
.eslintrc.js configuration description
Ssj-21b time relay
Understand the string class
扫雷小游戏——轻松玩上瘾(C语言版)
14.2 byte stream learning
Ds-24c/dc220v time relay
远程ip Debugger(实用干货)
Redis主从复制原理
2022.7.23-----leetcode.剑指offer.115
请问下有人知道FlinkSQL 的 Retrack 在哪里可以指定吗? 网上资料只看到API 代码设
uniapp h5、app引用外部在线js
Overseas app push (Part 2): Channel Integration Guide for overseas manufacturers
STM32驱动HC05蓝牙串口通信模块
Pytest interface automation test framework | pytest configuration file
Can the Windows version of Oracle be used in Linux?
什么是OOM,为什么会OOM及一些解决方法
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
The map function counts the number of occurrences of characters
