0

I want to build a simple function which forwards the browser to a specific destination if the route.name is Idx and if not, reloads the page. I use the following code and get the following error

I use vue 2-x.

Code

reloadPage() {
    if (this.$route.name == "Idx") {
        this.$router.push('/')
        //console.log("test")
    } else {
        this.$router.go() // RELOAD THE PAGE TO OVERTAKE THE CHANGES
    }            
}

error

Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/idx".

This error only appears when I run the function in the /idx site.

What do I miss?

Thanks!

2
  • window.location.reload(); Commented Nov 15, 2021 at 10:56
  • Hey Adam, thanks but if I replace the this.$router.go() with window.location.reload(); the page doesn't reload at all.... It works when I put the this.$router.go() in an extra function but that's not nice to look at Commented Nov 15, 2021 at 11:12

1 Answer 1

1

If your application didnot break with this error, one solution that i can think of is to handle the promise.

reloadPage() {
if (this.$route.name == "Idx") {
    this.$router.push('/').catch(()=>{});
    
} else {
    this.$router.go() // RELOAD THE PAGE TO OVERTAKE THE CHANGES
}            

}

Also, i tried running the same logic in the vue3 and didnt encounter this exception, i think they get rid of the same route navigation.

confirmInput() {
  // do something
  console.log('test', this.$router.currentRoute.value.path);
  if (this.$router.currentRoute.value.path === '/myroute') {
    this.$router.push('/');
  } else {
    this.$router.go();
  }
},
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ruben, the first answer worked for me and I will keep the second solution in mind since I will switch to vue3 soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.