Vue-router: pushing query params

Created on 25 Jul 2017  路  8Comments  路  Source: vuejs/vue-router

What problem does this feature solve?

As we can push router with query like router.push({query: {a: 1, b: 2}})

but it's not convenient while I just want to add a condition.

I do this as the followed code to get query string ?a=1&b=2&c=3:

import _ from 'lodash'
let query = _.cloneDeep(router.currentRoute.query)
query.c = 3
router.push(query)

What does the proposed API look like?

router.pushQueryParams({c: 3})

Most helpful comment

I'm not sure why you would need to deep-clone the query?

this.$router.push({ query: { ...this.$route.query, c: 3 }})

Frankly, since one of our goals is to keep API surface small, I don't think the small convenience of a few less characters to type warrants a new API.

All 8 comments

I'm not sure why you would need to deep-clone the query?

this.$router.push({ query: { ...this.$route.query, c: 3 }})

Frankly, since one of our goals is to keep API surface small, I don't think the small convenience of a few less characters to type warrants a new API.

I agree with @LinusBorg
On top of that, always replacing the query also allows to remove other query parameters

Yes. Maybe I'm using this in a wrong way.

Thank you anyway.

This can also be solved with adding something like this to your root component. For removing query parameters you can simply set a parameter to undefined

export default {
  name: 'App',
  watch: {
    '$route'(to, from) {
      if (from.name == to.name) {
        this.$router.replace({
          query: {
            ...from.query,
            ...to.query
          }
        })
      }
    }
  }
}

In Vue Router 3.2.0 intensive use of $router.replace fires Error: Avoided redundant navigation to current location

I'm currently using vue-router to store search filters in the query string of the url. This is so that a user can bookmark the page to reload their search filters or just share the page. I know I can get rid of the Error: Avoided redundant navigation to current location error by catching it and just ignoring it, but I feel that's bad practice and I'd like the code to fail on any other errors as well.

Is there a workaround or another approach that would allow me to update the query string using vue-router that would prevent navigation and thus prevent the error?

This can also be solved with adding something like this to your root component. For removing query parameters you can simply set a parameter to undefined

export default {
  name: 'App',
  watch: {
    '$route'(to, from) {
      if (from.name == to.name) {
        this.$router.replace({
          query: {
            ...from.query,
            ...to.query
          }
        })
      }
    }
  }
}

it did't work

I'm currently using vue-router to store search filters in the query string of the url. This is so that a user can bookmark the page to reload their search filters or just share the page. I know I can get rid of the Error: Avoided redundant navigation to current location error by catching it and just ignoring it, but I feel that's bad practice and I'd like the code to fail on any other errors as well.

Is there a workaround or another approach that would allow me to update the query string using vue-router that would prevent navigation and thus prevent the error?

Did you find a resolution to this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexstanbury picture alexstanbury  路  3Comments

shinygang picture shinygang  路  3Comments

lbineau picture lbineau  路  3Comments

marcvdm picture marcvdm  路  3Comments

saman picture saman  路  3Comments