ng和

时间:2018-04-30 11:40:02

标签: angular typescript

我在Angular 2 const声明中运行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无效的唯一原因是切片应用于元素,而不是列表(这很奇怪吗?)。

然而,尝试使用const。

迭代Foo.list时会产生乐趣
<li *ngFor="const element of foo.list"> ... </li>
                                ^ unexpected token
<li *ngFor="let element of foo.list"><!-- completely fine --></li>

我们什么时候应该使用const呢?我们为什么要在这些情况下使用let

0 个答案:

没有答案
相关问题