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)
要对简单变量以外的目标使用解构,您不能使用简写语法,但需要显式指定解构属性名称。在你的情况下:
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)
归档时间: |
|
查看次数: |
6509 次 |
最近记录: |