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

旧街凉风 提交于 2020-07-05 07:57:44

问题


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)


回答1:


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



回答2:


I stopped copy of this.$route object. That is works fine.



来源:https://stackoverflow.com/questions/52973554/vue-router-this-router-push-not-working-when-updating-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!