You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 18中如何重新赋值多个let变量?

如何在Angular 18中重新赋值多个let变量

你可以通过对象解构赋值的方式,一次性给已声明的多个变量重新赋值——你的函数fun返回的对象属性名刚好和变量名匹配,直接解构就能完成批量赋值,具体实现如下:

原代码:

let one,two,three; 
let fun=()=> {
  return ({one: "tt", two:"uu", three: "vv"})
}

添加解构赋值逻辑即可完成变量重新赋值:

let one, two, three; 
let fun = () => {
  return ({one: "tt", two:"uu", three: "vv"})
}

// 关键:用括号包裹解构表达式,避免被解析为代码块导致语法错误
({one, two, three} = fun());

执行后,one的值会变为"tt",two变为"uu",three变为"vv"。

内容的提问来源于stack exchange,提问作者EMahan K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:47:07