首页 > 解决方案 > 有没有办法在 Typescript 和 Angular 中为“let variable”类型输入提示 IDE?

问题描述

有像这样的标记

    <mat-cell *matCellDef="let request">
         <a [href]="request.url" target="_blank">{{request.requestId}}</a>
    </mat-cell>

我可以以某种方式输入提示 IDE 该请求是类型的Request吗?我在这里使用 IntelliJ。

请注意,我在这里使用 Angular Material 表,因此request在组件中声明不是一个选项,因为它纯粹是模板变量。它包含组件本身在每次行迭代时内部提供的行数据。

请注意,这是在 MatDataTable 组件中使用的完全有效的标记。

标签: angulartypescriptintellij-idea

解决方案


作为

tableDataSource: MatTableDataSource<ToDoInterface>;

不键入模型,

这个:

<ng-container matColumnDef="toDo">
  <th mat-header-cell *matHeaderCellDef mat-sort-header>ToDo</th>
  <td mat-cell *matCellDef="let model">
    <ng-container *ngIf="assertItemType(model) as toDoModel">
      {{toDoModel.toDo}}
    </ng-container>
  </td>
</ng-container>

在哪里:

assertItemType(item: ToDoInterface): ToDoInterface {
  return item;
}

作品。

但不确定这是否是最好的方法


推荐阅读