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

Viewed 18766

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

1 Answers

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}})
Related