gpt4 book ai didi

javascript - Angular2 : AsyncPipe and Observable, 无法检索属性(TypeError : Cannot read property . .. of null)

转载 作者:行者123 更新时间:2023-11-30 15:52:33 26 4
gpt4 key购买 nike

我尝试检索 Observable 返回的对象的属性

但是我在 SO 的答案中看到了一些示例,在我的应用程序中出现错误。同样的错误在 Plunker 中重现:

http://plnkr.co/edit/X5xNDOFmCpLOnzv4X0xo?p=preview

@Component({
selector: 'my-app',
providers: [AppService, HTTP_PROVIDERS],
template: `<h1>.?</h1>
<div *ngIf = "data$">
{{(data$ | async)?.total}}
</div>`
})
export class AppComponent {
data$ : Observable<any> = null;

constructor (private appservice : AppService) {}

ngOnInit(){
this.data$ = this.appservice.getData();
}
}

它建议我使用安全导航器操作符 (?.),但它没有帮助。

最佳答案

RC.5 引入了一个错误 https://github.com/angular/angular/issues/10639因此这目前不起作用。

作为解决方法,您需要在代码中订阅并分配给一个属性,例如

this.appservice.getData().subscribe(data => this.data = data);

然后绑定(bind)到data

关于javascript - Angular2 : AsyncPipe and Observable, 无法检索属性(TypeError : Cannot read property . .. of null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39094893/

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