【发布时间】:2021-09-16 09:12:59
【问题描述】:
我有一个 Angular 项目,在我的组件中,我使用 graphql 获取数据,并在 HTML 中显示结果的图像。但是有一会儿,我在控制台中收到了这个错误:
ERROR TypeError: Cannot read property 'image' of undefined
这是我的 html 代码:
<div
[style.background-image]="
'url(http://localhost:1337' + (data.article.image.url )+ ')'
"
>
</div>
在我的组件中我有:
ngOnInit(): void {
this.queryArticle = this.apollo
.watchQuery({
query: ARTICLE_QUERY,
variables: {
id: this.route.snapshot.paramMap.get("id")
}
}).valueChanges.subscribe(result => {
this.data = result.data;
this.loading = result.loading;
this.errors = result.errors
});
}
现在,我该如何避免这个错误?
【问题讨论】:
标签: angular graphql subscription apollo-angular async-pipe