gpt4 book ai didi

javascript - 可选链接不起作用 : Cannot read property '0' of undefined

转载 作者:行者123 更新时间:2023-12-05 01:10:23 25 4
gpt4 key购买 nike

为什么可选链在这里不起作用?

.html

 {{userItemModel?.item?.priceList[0]?.sellerUrl}}

它显示了这个错误。

TypeError: Cannot read property '0' of undefined

"@angular/core": "~10.1.1",

"typescript": "~4.0.2"

 "priceList": [
{
"amount": 14.5,
"currency": "USD",
"sellerUrl": "https://www.bay.com/itm/Lear-6910-/33372049",
"basePrice": 15,
"discount": 10
}
],

更新

用例 1:

  Parser Error: Unexpected token [, expected identifier or keyword at column 33 
in [ {{userItemModel?.item?.priceList?.[0]?.sellerUrl}}
] in

用例 2:

Parser Error: Unexpected token [, expected identifier or keyword at column 33 in [

{{userItemModel?.item?.priceList?.[0].sellerUrl}}
] in

最佳答案

要正确链接数组访问,您应该使用 arr?.[index]:

var foo = {}
try{
console.log(foo?.bar[0])
}
catch(e){
console.log(e.message)
}
console.log(foo?.bar?.[0])

编辑:这是 known issue此时有 Angular 。

关于javascript - 可选链接不起作用 : Cannot read property '0' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64104994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com