我尝试在导航到路线之前预先获取数据。当我转到其他路线然后返回target route
时,数据会附加到route.data
。我在阵列中有3个项目。当我第二次导航到路线时,有6个,第三个9,依此类推。
代码:
export class ItemsComponent implements OnInit {
constructor(
private route: ActivatedRoute) {
this.items$ = route.data.pluck('items');
}
// properties
items$: Observable<Item[]>;
}
export class ItemsResolver implements Resolve<Item[]> {
constructor(
private itemsService: ItemsService,
private router: Router) { }
resolve(): Observable<Item[]> {
return Observable.create((observer: Observer<Item[]>) => {
observer.next(this.itemsService.getItems());
observer.complete();
});
}
}
<div *ngFor="let item of items$ | async;"></div>