gpt4 book ai didi

javascript - 如何在 TypeScript 组件中使用 Observable 的值 | Angular 4

转载 作者:行者123 更新时间:2023-12-01 03:03:02 24 4
gpt4 key购买 nike

我正在尝试获取从 API 获得的 JSON 值并将其设置在变量中,例如:

TS

this.graphicService.getDatas().subscribe(datas => {
this.datas = datas;
console.log(datas);
});

test = this.datas[0].subdimensions[0].entry;

HTML

{{test}}

它在控制台上返回一个错误:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'subdimensions' of undefined
TypeError: Cannot read property 'subdimensions' of undefined
at new GraphicsComponent (graphics.component.ts:33)...

但是,如果我直接在 HTML 上使用,它确实有效,如下所示:

{{datas[0]?.subdimensions[0].entry}}

此时数据打印正确。..

最佳答案

试试这个:

let test;
this.graphicService.getDatas().subscribe(datas => {
this.datas = datas;
console.log(datas);
test = this.datas[0].subdimensions[0].entry;
});

关于javascript - 如何在 TypeScript 组件中使用 Observable 的值 | Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353604/

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