语法-变量声明中的方括号含义是什么 [英] Syntax - what does square brackets around a variable declaration mean

查看:580
本文介绍了语法-变量声明中的方括号含义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用以下代码行

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

推荐答案

它称为 解构分配 ,它用于解包array的值并将其分配给新变量.

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

因此在您的代码中:

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

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

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.

这篇关于语法-变量声明中的方括号含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆