【发布时间】:2018-04-27 12:15:46
【问题描述】:
取下面这行代码
const [component] = router.getMatchedComponents({ ...to })
谁能告诉组件周围的方括号在这里是什么意思?我试图用谷歌搜索,但很难找到答案
【问题讨论】:
标签: javascript arrays variables object declaration
取下面这行代码
const [component] = router.getMatchedComponents({ ...to })
谁能告诉组件周围的方括号在这里是什么意思?我试图用谷歌搜索,但很难找到答案
【问题讨论】:
标签: javascript arrays variables object declaration
它称为Destructuring assignment,用于解压array 的值并将它们分配给新变量。
所以在你的代码中:
const [component] = router.getMatchedComponents({ ...to })
您正在将array 中保存的第一个元素分配给component 变量,该元素将由router.getMatchedComponents({...to}) 返回,其中to 是一个类似数组的结构,使用展开操作转换为object。
【讨论】: