代码之家  ›  专栏  ›  技术社区  ›  Maciej Wojcik

使用异步管道可以释放对对象类型的引用

  •  1
  • Maciej Wojcik  · 技术社区  · 6 年前

    下面是一个简短的例子:

      <ng-container *ngIf="(state$ | async).foo as foo">
    

    实际上foo是 Foo: {id:string, name:string, value: number}

    foo 在模板IDE中,我们不知道foo有id、名称或值。

    Foo

    1 回复  |  直到 6 年前
        1
  •  3
  •   Muhammed Albarmavi    6 年前

    as foo 语句是创建一个不用于强制转换的模板变量,如果这样使用的话

       <ng-container *ngIf="(state$ | async).foo.id">
    

    您将获得intellisense类型,但是当您创建模板变量时,这些信息似乎丢失了。

    这可能是一个bug,将来可能会得到解决。

    <ng-container *ngIf="($state | async) as foo">
        {{foo | json}}
        <div>
            {{foo.id}} <!-- foo has no type information-->
        </div>
    
      {{value.name}} <!-- declared property has type information-->
    </ng-container>
    

    stackblitz demo