[英]let keyword always show not defined error in typescript
I have below part of code:我有以下部分代码:
if (this.holdReasonList.length == 1 || this.holdReasonList == undefined || this.holdReasonList.length == 0) {
return this._cciDataService.getHoldReasonDropList()
.pipe(takeUntil(this.unsubscribe))
.subscribe((res) => {
if (res.Success) {
let defaultList = [];
res.Data.forEach((reason: string) => {
defaultList.push(reason.trim());
})
I always got undefined error of defaultList
after I called this line:调用此行后,我总是得到未定义的defaultList
错误:
let defaultList = [];
I do not understand why defaultList
is not defined since I clearly declared it.我不明白为什么defaultList
没有定义,因为我明确声明了它。
Thanks for the help in advance!我在这里先向您的帮助表示感谢!
UPDATE:更新:
Thanks for the advice!感谢您的建议! Since I tried put the let defaultList=[]
outiside the whole piece of the subscribe block, it works now.由于我尝试将let defaultList=[]
放在整个订阅块之外,它现在可以工作了。
But the reason is not clear.但原因尚不清楚。 I set the break point and type let defaultList = []
in the console and it also returns undefined.我设置断点并在控制台中输入let defaultList = []
,它也返回未定义。 Please see below screenshot:请看下面的截图:
I think I do not need to even think of where the defaultList
to be used, the defualtList
is undefined right after the declaration.我想我什至不需要考虑要在哪里使用defaultList
, defualtList
在声明之后是未定义的。 I do not have any chance to even use its value....我什至没有机会使用它的价值......
Ok.好的。 When I debug on Chrome devTool, the variable is defined....The issue was that I was debugging by using VSCode.当我在 Chrome devTool 上调试时,定义了变量....问题是我正在使用 VSCode 进行调试。
Be aware that let
keyword will cause undefined variable on VSCode debugger.请注意let
关键字将导致 VSCode 调试器上未定义的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.