为什么可选链在这里不起作用?
.html
{{userItemModel?.item?.priceList[0]?.sellerUrl}}
它显示此错误。
TypeError: Cannot read property '0' of undefined
“@角/核心”:“〜10.1.1”,
“打字稿”:“~4.0.2”
"priceList": [
{
"amount": 14.5,
"currency": "USD",
"sellerUrl": "https://www.bay.com/itm/Lear-6910-/33372049",
"basePrice": 15,
"discount": 10
}
],
Update
用例 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])
编辑:这是一个已知问题 https://github.com/angular/angular/issues/34385此时有角度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)