当前位置:网站首页>The programmed navigation route jumps to the current route (the parameters remain unchanged), and if it is executed multiple times, it will throw a navigationduplicated warning error?
The programmed navigation route jumps to the current route (the parameters remain unchanged), and if it is executed multiple times, it will throw a navigationduplicated warning error?
2022-07-26 12:34:00 【Ha ha Da ( ̄▽  ̄) '】
>> rewrite push and replace Method
1. Program navigation route to jump to the current route ( The parameters are the same ), Multiple executions will throw NavigationDuplicated Warning error ?
-- There are two forms of route jump : Declarative navigation 、 Programming navigation
-- Declarative navigation has no such problems , because vue-router The bottom has been handled .
1.1 Why is it that when programmed navigation performs route jump , There are such warning errors ?
"vue-router":"^3.5.3": Abreast of the times vue-router introduce promise
1.2 By giving push Method passes the corresponding success 、 Failed callback function , You can catch the current error , Can solve .
1.3 Through the code at the bottom , It can solve the error
this.$router.push({
name:'search',
params:{
keyword:this.keyword
},
query:{
k:this.keyword.toUpperCase()
}
},()=>{
},(error)=>{
console.log(error)
})
This kind of writing : Treat the symptoms, not the root cause , In the future, among other components push|replace, There are similar errors in programming navigation
1.4
return promise. Why did the above warning appear ? because promise You need to pass a successful or failed callback , In this way, these exceptions can be handled .
this: The current component instance (search)
this.$router attribute : The current property , The property value is VueRouter An instance of a class , When registering a route in the entry file , Add... To the component instance $router|$route attribute
this keyword , That is, the so-called execution context
push:VueRouter An instance of a class
function VueRouter(){
}
VueRouter.prototype.push = function(){
// The context of the function is VueRouter An instance of a class
}
// How to prototype objects
let $router = new VueRouter()
$router.push(xxx)
this.$router.push();(1) The first VueRouter Of a prototype object push, Save a copy first (2) rewrite router.prototype Upper push Method (3) If the external input resolve and rejected Callback , Then use the external input ; Otherwise, an empty function is passed in as the return transfer , In this way, you can avoid the error when there is no incoming callback . (4) Use call Why : because originPush It has been extracted as a global function , if push When called , Don't explicitly specify this, be this Will point to window, instead of VueComponent example .
// The first VueRouter Of a prototype object push, Save a copy first
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
// rewrite push|replace
// The first parameter : Tell the original push Method , Where do you jump ( What parameters are passed )
// The second parameter : Successful callback
// The third parameter : Failed callback
//call||apply difference
// The same thing , Can call the function once , Can tamper with the context of a function once
// Difference :call And apply Pass parameters ;call Pass parameters separated by commas ,apply Method execution , Pass array
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 and apply difference
- The same thing , Can call the function once , Can tamper with the context of a function once
- Difference :call And apply Pass parameters ;call Pass parameters separated by commas ,apply Method execution , Pass array
边栏推荐
- .NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
- Jsj-3/ac220v time relay
- UE5 官方案例Lyra 全特性详解 7.资源管理
- 基于STM32的SIM900A发送中文和英文短信
- The "2022 Huawei developer competition eastern China division opening ceremony" was successfully held in Fuzhou
- Sim900a based on STM32 sends short messages in Chinese and English
- 【2243】module_ param.m
- Knowledge points of C language documents
- Uniapp H5, APP references external online JS
- Data query where
猜你喜欢

Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)

Knowledge points of C language documents

Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)

Huawei ultra fusion fusioncube solution notes

Paper reading MLPD: multi label pedestrian detector in multispectral domain (Internship Program of Hikvision Research Institute)

二、容器_

PXE principle and configuration

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

Some common writing methods and skills

编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
随机推荐
Tencent cloud and smart industry business group (CSIG) adjusted the organizational structure and established the digital twin product department
“2022华为开发者大赛中国区东部赛区开幕式”在福州成功举办
Flutter's learning path
详解勒让德变换与共轭函数
数据库组成表
回溯——131. 分割回文串
You Yuxi recommends vite to beginners [why use vite]
2022 年要了解的新兴安全供应商
Beauty salon management system unified management system?
数字化时代,是什么“黄金宝藏”在推动百年药企发展?
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
Real time synchronization and conversion of massive data based on Flink CDC
Customize browser default right-click menu bar
Database composition table
Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
什么是物联网?常见IoT协议最全讲解
论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)
Azure Synapse Analytics 性能优化指南(2)——使用具体化视图优化性能(上)
Oracle的Windows版本能在linux中使用吗?
Some common writing methods and skills