修改vue路由对象($route)的query,亲测有效

162 阅读1分钟

需求:从首页搜索框输入关键词,带着关键词跳转至搜索列表页(本人使用路由携带参数进行关键词的传递),导致搜索列表页跳转至详情再回退后,搜索关键词并非最新的那一个。

首页:

this.$router.push({path:'/searchList', query: {keywords:this.keywords}})

于是用这种方法解决:在query中增加时间戳,以保证每次回退时候,关键词都是最新的且路由不重复

搜索列表页:

this.$router.push({    path: this.$route.path,    query: Object.assign({}, this.$route.query, { keywords:this.keywords, _: +new Date() })})