我需要解构一个对象来更新数据中定义的变量。

问题描述 投票:0回答:1

这是我的第一篇帖子,我需要对一个定义在 "data "中的变量进行重构更新,我有以下代码片段。我需要重构更新一个定义在 "data "中的变量,我有以下代码片段。我使用的是VUE。

data: () => ({
    id: '',
    phone: '',
    email: ''
}),
methods: {
 async getId(){
   {this.id, this.email, this.phone} = this.$route.query.item
 }
}
javascript vue.js object ecmascript-6 destructuring
1个回答
2
投票

其实你可以对现有的变量进行赋值。

只是语法有点奇怪。

这个应该可以

({id: this.id, phone: this.phone, email: this.email} = this.$route.query.item)

这里有一个 实例


1
投票

你不能对现有的道具进行破坏,只能对新道具进行破坏。

data () {
    return {
     item: {
       id: '',
       phone: '',
       email: ''
     }
   }
},
...
methods: {
 async getId(){
   { id, email, phone } = this.$route.query.item
   Object.assign(this.item, { id, email, phone })
© www.soinside.com 2019 - 2024. All rights reserved.