7

Current url is

/?type=1

I want to use router.push on this page.

this.$router.push('/?type=2');

But this gives NavigationDuplicated error.

I don't want to use params like

/:type
1
  • 1
    So how did you solve it? You made some comments that you were able to solve it, but you don't say how. Please explain. Commented Oct 12, 2021 at 16:25

5 Answers 5

10

Aliraza already answered your questions but I am sharing this answer because I see your comments asking, component don't rerender, for rerendering components you need to watch params like this:

 watch: {
    '$route.params': 'functionToRunWhenParamsChange',
  }
Sign up to request clarification or add additional context in comments.

1 Comment

A solution that works for me. But for anyone want to use this method, be aware to add null checker, because when we leave the route, this callback would be called again and the params is undefined this time.
3

That's because it's not different from your current path. If the value of type is different it won't give you NavigationDuplicated error. you can separate query like below too to make sure it will be understandable by the framework:

this.$router.push({
   path: '/',
   query: { type: 2 },
});

4 Comments

This doesn't re-render the component.
OK, anyway I found how to control NavigationDuplicated error. I need to make another question for the component issue. Thank you!
This doesn't actually work as pointed out by @JeongyongLee the router does not push to the same path although the query has changed
This does not solve the issue. Rather navigates you to the root of your router and does not stay in the same path but with new params.
3
<router-view :key="$route.fullPath"

Comments

2

Please see this GitHub issue. They're using this.$router.replace but I imagine it shares the same root cause as when you are using this.$router.push. The proposed solution / workaround is to use an empty catch chained to the $router call, so:

this.$router.push('/?type=2').catch(err => {})

EDIT

You can pass the query params as an object to the second parameter of this.$router.push as well:

this.$router.push({ query: { type: 2 } })

2 Comments

This doesn't re-render the component.
OK, anyway I found how to control NavigationDuplicated error. I need to make another question for the component issue. Thank you!
-5

If you wanna change your url (I mean push to another web page) and reload that page, you should use this syntax:

window.location.href = "your-url"

GG

1 Comment

OP asked about VueJS, not regular JS.

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.