vue-router: this.$router.push not working when updating query

17,002

You shouldn't try to push to the route object. Instead you should use one of these:

// literal string path
router.push('home')

// object
router.push({ path: 'home' })

// named route
router.push({ name: 'user', params: { userId: 123 }})

// with query, resulting in /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})

In your case:

this.$router.push({path: this.$route.path, query: {disp: disp}})
Share:
17,002
narupo
Author by

narupo

フリーのプログラマーをやっているnarupoです。

Updated on June 14, 2022

Comments

  • narupo
    narupo almost 2 years

    I want to update query on url. But, $router.push does not updating url. disp is different value for each call. Why?

    handle: function(disp) {
        let route = Object.assign({}, this.$route);
        route.query.disp = disp;
        this.$router.push(route);
    },
    

    versions.

    "vue": "^2.5.17",
    "vue-router": "^3.0.1",
    

    route (console.log)

    enter image description here

  • narupo
    narupo over 5 years
    I tryed this.$router.push({ name: 'name', params: { page: this.$route.params.page }, query: { disp: [dispLines,] } });. But, url is not updating. But, this.$route was updated by push parameters. Why url is not updating?
  • narupo
    narupo over 5 years
    And I tryed this.$router.push({ path: '/posts', query: { disp: [dispLines,] } });. That is not working.
  • Luckylooke
    Luckylooke almost 5 years
    It does not work for me as well... when I am pushing same path, url is not getting updated, when I push another path it works well.. but I want to stay on same path, just need to update param :/ "vue": "2.6.10", "vue-router": "3.0.6"
  • Luckylooke
    Luckylooke almost 5 years
    I have found my problem... this answer works fine I have test it here codesandbox.io/s/vue-template-9o3u5. My problem is that I am copying query params between routes on each route change and it was the cause.