vue路由请求参数被改变

this.query = this.$route.query;
this.getList(this.query);
getList(data){
  data.method = 'get'
}

为什么这样传值,后面我再请求this.$route.query的时候发现多了个medthod:'get'字段呢,如何解决这种问题?

阅读 5.3k
2 个回答

JSON.parse(JSON.stringify(this.$route.query))

因为你调用this.getList(this.query)中传入的是引用,也就是在函数内部data.method = 'get'相当于this.query.method = 'get',也相当于this.$route.query.method = 'get'


如果单纯想使用this.$route.query中的内容而不想改变原有对象的话,可以对getList函数做一点修改:

getList ({...data}) {
    data.method = 'get'
}

这里需要保证getList的参数是一个对象,方便收集到data中。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题