我在使用带有严格模式的14号角的trackBy时遇到了问题:True,
这是代码(它的工作方式类似于严格模式下的charme:FALSE)
App.Component.html:
<div *ngFor="let item of items; index as i; trackBy: trackByFn">
{{ i + 1 }} {{item.name}}
</div>
<br>
<button (click)="addSomething()">Load Data</button>
App.components.ts
import { Component } from '@angular/core';
interface DemoArray {
id: number;
name: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'angular-sandbox';
public items: DemoArray[] = [
{ id: 0, name: 'Apfel' },
{ id: 1, name: 'Mango' },
{ id: 2, name: 'Birne' },
];
public addSomething(): void {
this.items = [
{ id: 0, name: 'Apfel' },
{ id: 1, name: 'Mango' },
{ id: 2, name: 'Birne' },
{ id: 3, name: 'Kiwi' },
{ id: 4, name: 'Mandarine' },
{ id: 5, name: 'Banane' },
];
}
trackByFn(item) {
return item.id;
}
}
使用严格模式:True并将trackByFn更改为:
trackByFn(item: { id: any }) {
return item.id;
}
VSCode说:
类型‘(Item:{id:any;})=>;any’不可赋值给类型‘TrackByFunction’.ngtsc(2322) App.Component.ts(8,44):组件AppComponent的模板出错.