Vue - 解构对象(数据)

Fan*_*mic 3 javascript destructuring ecmascript-6 vue.js

如何对 Vue 组件数据使用对象解构?这是我的实际代码:

data: () => ({
  descricao: '',
  perfilBase: null,
  anotherField: '',
  otherOne: false
}),

mounted () {
  this.descricao = this.$route.query.descricao
  this.perfilBase = this.$route.query.perfilBase
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这样呢?是否可以?

mounted () {
  { this.descricao, this.perfilBase } = ...this.$route.query
}
Run Code Online (Sandbox Code Playgroud)

Ber*_*rgi 5

要对简单变量以外的目标使用解构,您不能使用简写语法,但需要显式指定解构属性名称。在你的情况下:

mounted () {
  ({ descricao: this.descricao, perfilBase: this.perfilBase } = this.$route.query);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果query对象仅包含这两个属性,Object.assign则解决方案要简单得多:

mounted () {
  Object.assign(this, this.$route.query);
}
Run Code Online (Sandbox Code Playgroud)