JS对象在新对象内部进行解构

时间:2017-05-09 09:42:44

标签: javascript ecmascript-6

我试图通过对象解构来实现这一目标,但却无法找到正确的语法:

没有解构:

  this.$store.dispatch('user/login', {
    username: this.username,
    password: this.password
  })

随着解构(不起作用):

  this.$store.dispatch('user/login', {
    { username, password } = this
  })

这里的语法是什么?

1 个答案:

答案 0 :(得分:4)

我觉得你很困惑。这里没有“destructure” - 你的dispatch函数只需要一个带有2个属性的对象参数(usernamepassword)。

您可以在调用之前定义此对象,如:

var input = {username:this.username,password:this.password};
this.$store.dispatch('user/login', input);

或者,由于您当前的对象具有正确的属性,因此您只需传递this

即可
this.$store.dispatch('user/login', this);

通过解构的文档here来理解它是值得的。

相关问题