使用异步管道会丢失对对象类型的引用

问题描述 投票:1回答:1

我有一个简单的问题。当我在模板IDE中使用异步管道时,不知道异步管道中的对象是什么类型。

这是一个简短的例子:

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

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

问题是,当我想在模板IDE中使用foo时,不知道foo有id,name或value。

是否有任何干净的解决方案“施放”fooFoo

angular angular-pipe
1个回答
1
投票

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

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

你会得到intellisense类型,但是当你创建一个模板变量时,这些信息似乎就丢失了。

这考虑了一个错误,将来可能会解决。

<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

© www.soinside.com 2019 - 2024. All rights reserved.