简体   繁体   English

新对象内部的JS对象分解

[英]JS Object destructuring inside new object

I'm trying to achieve this with object destructuring but can't quite figure out the right syntax: 我正在尝试通过对象分解来实现这一点,但是我还不太清楚正确的语法:

Without destructuring: 不破坏结构:

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

With destructuring (doesnt work): 进行销毁(无效):

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

What's the right syntax here? 这里正确的语法是什么?

I think you're confused. 我觉得你很困惑。 There is nothing to "destructure" here - your dispatch function simply takes an object parameter with 2 properties ( username and password ). 这里没有什么可“解构”的-您的dispatch功能仅采用具有2个属性( usernamepassword )的对象参数。

You could define this object before calling it like: 您可以在调用之前定义该对象,如下所示:

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

Or, as your current object has the right properties you could simply pass this 或者,作为当前对象有正确的属性,你可以简单地通过this

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

It's worth going though the docs here on destructuring to understand it. 值得通过这里的有关分解的文档来理解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM