SoFunction
Updated on 2025-03-09

vue-router rewrites push method to solve the problem of jumping errors in the same path

Modify the configuration file of vue-router, default location router/

import Vue from 'vue'
import Router from 'vue-router'
 
/**
  * Push method for rewriting routes
  * Solved, an error was reported when the same route jumps
  * Add, when the same route jumps, the watch is triggered (for el-menu, only the string method is passed, like "view?id=5")
  */
 
// Save the original push functionconst routerPush =  
// Rewrite the push function = function push(location) {
 
 // When this if statement jumps to the same path, adds new parameters (some random numbers) at the end of the path // Used to trigger the watch if(typeof(location)=="string"){
 var Separator = "&";
 if(('?')==-1) { Separator='?'; }
 location = location + Separator + "random=" + ();
 }
 
 // This statement is used to solve the error // Call the original push function and catch the exception return (this, location).catch(error => error)
}
 
(Router)
export default new Router({
 routes: [
 {
  path: '/',
 
 }
 ]
})

Supplementary knowledge:vue router-link path changes page content remains unchanged

I have encountered this in VUE project. If you use router-link or () to access the same routing page, the URL address will change, but the page content has not changed and the information is not reloaded.

The solution is as follows

<router-link to="/home" @="flushCom">front page</router-link>
<script>  
export default {
...
...
methods:{
 flushCom:function(){
 //router is a routing instance, for example: var router = new Router({})  //(n) is a method of routing, which means how many steps forward or backward in the history record, 0 means whether it is still the current one, similar to (n) this.$(0); 
 }
}
}
<script>  

It is equal to refresh this page after the router link is triggered and click time is used.

The above article vue-router rewrites the push method to solve the problem of jumping to the same path error is all the content I share with you. I hope you can give you a reference and I hope you can support me more.