Vue-router does not allow navigation to the current location (/path) Error reasons and fixes

Vue-router does not allow navigation to the current location (/path) Error reasons and fixes

Error message

Navigating to current location ("/path") is not allowed

Navigating to current location ("/path") is not allowed

Cause

When the console displays the above message Navigating to current location ("/path") is not allowed , it is because the same route is entered repeatedly.

Error demonstration

In order to demonstrate the error, a new project was rebuilt with vue-cli , and only one按鈕and one input were written.
code:

<!-- vue template code -->
<div>
	<button @click="gotoHandle">Test route jump</button>
	<input v-model="routeName">
<div>
// Routing code export default {
  data() {
    return {
      routeName: ''
    }
  },
  methods: {
    gotoHandle() {
      this.$router.push({name: this.routeName})
    }
  }
}

Animated demo

Navigating to current location ("/path") is not allowed

When you enter the same route repeatedly (whether by path or route name), you will be prompted that navigation to the current location ( path ) is not allowed. For example, in the example above, when entering the route named About , the prompt is path: "/about" and Navigating to current location ("/about") is not allowed. This is because when the jump method is wrong, the error handling is not captured, so the error information is directly output.

Workaround

Method 1

Add .catch(error => error) directly to the place where the error is reported

export default {
  data() {
    return {
      routeName: ''
    }
  },
  methods: {
    gotoHandle() {
      this.$router.push({name: this.routeName}).catch(error => error)
    }
  }
}

Method 2

Add global error capture to the method that jumps to the error.

import VueRouter from 'vue-router'

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return routerPush.call(this, location).catch(error => error)
}

The above code is the same when executed in main.js or router/index.js , and before and after new VueRouter . Because it is a push event on the reset VueRouter prototype object, a capture exception is added to push event of the prototype object, so all related objects will be changed through the prototype chain.

The duplicate jump error of the replace method is similar to the above. Just change push replace .

Method 3

This method is recommended. It is recommended to optimize the jump logic to avoid repeated jumps to the same route.

This is the end of this article about the error cause and fix of Vue-router not allowing navigation to the current location (/path). For more related Vue-router navigation to the current location content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use vue3.x+vite+element-ui+vue-router+vuex+axios to build a project
  • A super detailed Vue-Router step-by-step tutorial
  • The difference between hash mode and history mode in vue-router
  • Detailed explanation of the installation and use of Vue-Router
  • Complete steps to use vue-router in vue3
  • Vue-router routing lazy loading and 3 ways to implement it
  • Detailed explanation of the difference between hash mode and history mode in Vue-router
  • vue-router defines meta information meta operation
  • Initial practice of vue3.0+vue-router+element-plus
  • Detailed explanation of vue-router's navigation hook (navigation guard)
  • Vue-Router installation process and principle detailed

<<:  Analysis of the new features of MySQL 8.0 - transactional data dictionary and atomic DDL

>>:  Use of Linux ls command

Recommend

How to implement scheduled backup of MySQL in Linux

In actual projects, the database needs to be back...

MySQL FAQ series: When to use temporary tables

Introduction to temporary tables What is a tempor...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

Introduction to the use of http-equiv attribute in meta tag

meta is an auxiliary tag in the head area of ​​htm...

Zabbix configuration DingTalk alarm function implementation code

need Configuring DingTalk alarms in Zabbix is ​​s...

How to convert JavaScript array into tree structure

1. Demand The backend provides such data for the ...

In-depth understanding of MySQL slow query log

Table of contents What is the slow query log? How...

Nginx learning how to build a file hotlink protection service example

Preface Everyone knows that many sites now charge...

CSS3+HTML5+JS realizes the shrinking and expanding animation effect of a block

When I was working on a project recently, I found...

Nginx restricts IP access to certain pages

1. To prohibit all IP addresses from accessing th...

Mysql database design three paradigm examples analysis

Three Paradigms 1NF: Fields are inseparable; 2NF:...

MySQL encryption and decryption examples

MySQL encryption and decryption examples Data enc...

Ubuntu 18.04 installs mysql 5.7.23

I installed MySQL smoothly in Ubuntu 16.04 before...