angular - ngFor 中的 let 和 const 修饰符行为细节
问题描述
const
我在 Angular 2*ngFor
语句中运行了一系列意想不到的修饰符行为。
假设我有以下基本代码:
interface Foo {
name: string;
list: string[];
}
@Component({ ... })
class FooComponent() {
@Input foo: Foo;
@Input list: string[];
}
在以下模板中使用const
在编译时和运行时完全没问题。
<li *ngFor="const element of list"> ... </li>
现在,如果我们使用 SlicePipe,它将const
不再工作,并且 TypeScript 编译器会在|
标记处引发错误。
<li *ngFor="const element of list | slice:0:3"> ... </li>
^ unexpected token
<li *ngFor="let element of list | slice:0:3"><!-- completely fine --></li>
我期待管道应用于列表,我能找到const
无效的唯一原因是切片应用于元素,而不是列表(这很奇怪吗?)。
Foo.list
然而,当尝试使用 const进行迭代时,乐趣就来了。
<li *ngFor="const element of foo.list"> ... </li>
^ unexpected token
<li *ngFor="let element of foo.list"><!-- completely fine --></li>
那我们应该什么时候使用const
呢?为什么要let
在这些情况下使用?
解决方案
推荐阅读
- python - 如何使用日期时间索引删除 DataFrame 中的多个时间段?
- javascript - 设置范围保护每周基于时间的触发
- javafx - 将参数传递给扩展服务类 Java 的类
- java - 通过枚举的其他值设置枚举值
- python - 在 Python 中更改图片的对比度和锐度
- python - 张量元组的输入形状错误
- android - Android INSTALL_FAILED_VERSION_DOWNGRADE 没有安装以前的应用程序
- python - 找出整数池中的最大数
- powershell - 后台中的 Powershell 循环作业
- linux - 安装错误版本的 cmake 的 Bitbucket 管道