从Url中删除查询字符串参数

时间:2017-07-21 09:52:55

标签: javascript angularjs vue.js vuejs2

来自AngularJS我认为这在Vue.js 2中也很容易。但在Vue中,这似乎很难设计。

在AngularJS中,我可以执行此$location.search('my_param', null);,这将有效地将https://mydomain.io/#/?my_param=872136转换为https://mydomain.io/#/

在Vue中,我尝试了this.$router.replace('my_param',null);,但它只会https://mydomain.io/#/?my_param=872136 - > https://mydomain.io/#/my_param,留空 my_param

在Vuejs2中是否还没有从Url中删除查询参数?我应该使用普通的JS来实现这个目标吗?

2 个答案:

答案 0 :(得分:3)

router.replace()是通过从浏览器历史记录堆栈中删除当前URL并将其替换为您传递给它的参数路由来导航。

实际语法为router.replace(url_location, onComplete, onAbort)

您正在做的是router.replace(my_param, null),它会从历史记录堆栈中删除当前网址并将其替换为'my_param',而onComplete回调您正在传递null

所以这样做:

this.$router.replace('/')

有关programatic navigation

的更多信息

答案 1 :(得分:2)

如果您有多个查询参数,删除其中一个参数的正确方法是:

const query = Object.assign({}, this.$route.query);
delete query.my_param;
this.$router.replace({ query });