【问题标题】:Syntax - what does square brackets around a variable declaration mean [duplicate]语法 - 变量声明周围的方括号是什么意思[重复]
【发布时间】:2018-04-27 12:15:46
【问题描述】:

取下面这行代码

const [component] = router.getMatchedComponents({ ...to })

谁能告诉组件周围的方括号在这里是什么意思?我试图用谷歌搜索,但很难找到答案

【问题讨论】:

标签: javascript arrays variables object declaration


【解决方案1】:

它称为Destructuring assignment,用于解压array 的值并将它们分配给新变量。

所以在你的代码中:

const [component] = router.getMatchedComponents({ ...to })

您正在将array 中保存的第一个元素分配给component 变量,该元素将由router.getMatchedComponents({...to}) 返回,其中to 是一个类似数组的结构,使用展开操作转换为object

【讨论】:

  • 谢谢!所以这意味着在我发布的示例中,它本质上是对 router.getMatchedComponents({ ...to }) 返回的数组进行数组移位并将组件设置为此正确吗?
  • @Neil 是的,有点。但它会将其分配给一个新变量。
猜你喜欢
  • 2014-10-01
  • 2010-10-01
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多