从网址中删除查询字符串参数

Mar*_*cus 5 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中是否没有要从网址中删除查询参数?我是否应该使用纯JS来实现这一目标?

nbw*_*ard 24

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

const query = Object.assign({}, this.$route.query);
delete query.my_param;
this.$router.replace({ query });
Run Code Online (Sandbox Code Playgroud)


Vam*_*hna 11

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

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

您正在做的是router.replace(my_param, null)从历史堆栈中删除当前 URL 并将其替换'my_param'onComplete您正在传递的回调null

所以这样做:

this.$router.replace('/')
Run Code Online (Sandbox Code Playgroud)

有关程序导航的更多信息

  • 对于只需要清除查询参数而不影响路由器堆栈的人,请尝试以下操作:`this.$router.replace({'query': null});` (18认同)