Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve Avoided redundant navigation to current location error in vue?

Tags:

vue.js

I am new in vue and i got the error after user logged in and redirect to another route. Basically i am a PHP developer and i use laravel with vue. Please help me to solve this error.

Uncaught (in promise) Error: Avoided redundant navigation to current location: "/admin".

Here is the screenshot too

enter image description here

Vue Code

 methods: {         loginUser() {             var data = {                 email: this.userData.email,                 password: this.userData.password             };             this.app.req.post("api/auth/authenticate", data).then(res => {                 const token = res.data.token;                 sessionStorage.setItem("chatbot_token", token);                 this.$router.push("/admin");             });         }     } 

Vue Routes

const routes = [     {         path: "/admin",         component: Navbar,         name: "navbar",         meta: {             authGuard: true         },         children: [             {                 path: "",                 component: Dashboard,                 name: "dashboard"             },             {                 path: "users",                 component: Users,                 name: "user"             }         ]     },     {         path: "/login",         component: Login,         name: "login"     } ];  const router = new VueRouter({     routes,     mode: "history" });  router.beforeEach((to, from, next) => {     const loggedInUserDetail = !!sessionStorage.getItem("chatbot_token");     if (to.matched.some(m => m.meta.authGuard) && !loggedInUserDetail)         next({ name: "login" });     else next(); });  
like image 520
Robin Singh Avatar asked Jun 19 '20 02:06

Robin Singh


People also ask

How do I redirect my route on Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.

How do I refresh my Vue page?

To reload a route with Vue Route, we can call the this. $router.go() method. If it has no arguments, then it'll reload the current route. This way, it'll notice when the path changes and it'll trigger a reload of the component with new data.

How route parameter can be accessed in the Vue app?

Access a route parameter with the $route instance object The Vue router gives us access to the $route instance object. On this object is the params property that we can use to access the parameter from the URL. The property name has to be the same as the one we define in the route path.

What is $route in Vue?

Vue router: Vue Router helps link between the browser's URL/History and Vue's components allowing for certain paths to render whatever view is associated with it. A vue router is used in building single-page applications (SPA). The vue-router can be set up by default while creating your new project.


2 Answers

As I remember well, you can use catch clause after this.$router.push. Then it will look like:

this.$router.push("/admin").catch(()=>{}); 

This allows you to only avoid the error displaying, because browser thinks the exception was handled.

like image 61
bartekwaliszko Avatar answered Sep 18 '22 18:09

bartekwaliszko


I don't think suppressing all errors from router is good practice, I made just picks of certain errors, like this:

router.push(route).catch(err => {     // Ignore the vuex err regarding  navigating to the page they are already on.     if (       err.name !== 'NavigationDuplicated' &&       !err.message.includes('Avoided redundant navigation to current location')     ) {       // But print any other errors to the console       logError(err);     }   }); 
like image 24
Luckylooke Avatar answered Sep 19 '22 18:09

Luckylooke