4

currently have query param id=2&p=3 want to change this with vue-router tried :

this.$router.push({query:{id:'2',p:'4'}});

but throws NavigationDuplicated

weird ..

how to change just the query, to trigger watch.

4
  • 1
    shouldn't you provide route name? Commented Sep 25, 2019 at 17:47
  • tried that,, even that is not working .. same error Commented Sep 26, 2019 at 6:01
  • there's a discussion on github github.com/vuejs/vue-router/issues/2881, maybe it can fix your problem Commented Sep 26, 2019 at 12:57
  • 1
    Did you find a solution? I'm having the same problem Commented Apr 23, 2020 at 20:27

3 Answers 3

2

The error will be thrown only if your params the same, so you could just check your params before push or replace. Also, you could use async/await or then/catch with there methods, here is an example:

try {
  if (/* id or p have been changed in this.$route.query */) {
    await this.$router.push({query:{id:'2',p:'4'}});
  }

} catch (err) {
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

my question is even if id or p changes it throws up error
1

I had the same issue of getting a "NavigationDuplicated" error on adjusting my query. In my case the problem was because on page load I was naively getting the query object directly from the router. I loaded it like:

beforeRouteEnter (to, from, next) {
  store.dispatch('setQuery', to.query);
  next();
}

where setQuery would set my vuex store variable query. The issue is then that this object just points to the query inside the route object, and making changes to it changes directly route.query but the url doesn't react to this change. If you then do something like:

watch: {
  query (query) {
    this.$router.push({query: query})
  }
}

you will get a "NavigationDuplicated" error as the new "query" will be identical to the old "query" since you are just passing the pointer to the original object.

To avoid this issue you can do some sort of deep copy (e.g. JSON.parse(JSON.stringify(to.query))) to make sure you are not directly modifying the route.query when you adjust the query.

Comments

0

if you create button and want to router push, just convert those button to <router-link :to="routeObject" />. Router-link will not react if destination route = current route, so it won't show error navigationDuplicate

note:

routeObject : {
  path : '/search',
  query : {}
}

Comments

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.