Syntax - what does square brackets around a variable declaration mean [duplicate]

∥☆過路亽.° 提交于 2019-12-18 05:57:28

问题


Take the following line of code

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

Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer


回答1:


It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.

So here in your code:

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

You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.



来源:https://stackoverflow.com/questions/47287635/syntax-what-does-square-brackets-around-a-variable-declaration-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!