【问题标题】:I seem to be facing an error with using the async pipe operator我似乎在使用异步管道运算符时遇到了错误
【发布时间】:2026-02-04 09:30:01
【问题描述】:
 <div class="image" *ngFor="let post of (userData | async)?.posts"></div>

错误信息: 未定义标识符“userData”。组件声明、模板变量声明和元素引用不包含这样的成员。

【问题讨论】:

  • 给出更多关于错误和组件类文件的细节
  • 我们需要你的 .ts 文件来读入它,但另外你可以先将管道与其他管道分开,然后使用 *ngIf = "userData | async as user" 切换到一个 div,所以然后您应该在模板中包含您的同步数据
  • 是否有 (userData | async)?.posts 问号用于可选链接?
  • 谢谢,但我设法解决了错误
  • 我在 ts 文件中创建的属性不是我在 html 页面中链接到的属性

标签: angular typescript


【解决方案1】:

您的组件类TS文件中似乎没有userData。你能发布你的组件文件吗?

【讨论】:

    【解决方案2】:

    我们可以通过这段代码来处理吗?

       <ng-container *ngIf="userData.length > 0">
       <div class="image" *ngFor="let post of (userData | async).posts"></div>
       <ng-container>
    

    【讨论】: