语法 - 变量声明周围的方括号是什么意思

时间:2017-11-14 13:55:40

标签: javascript arrays variables object declaration

采取以下代码行

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

有人可以告知组件周围的方括号是什么意思吗?我试图谷歌这个但很难找到答案

1 个答案:

答案 0 :(得分:3)

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

所以在你的代码中:

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

您正在声明一个component变量,该变量将保留将从router.getMatchedComponents({...to})返回的数组中的对象保留,其中to是一个类似于{{1}的结构的数组使用传播操作。