angular – 在地图后显示可观察的
发布时间:2020-12-17 16:59:32 所属栏目:安全 来源:网络整理
导读:我尝试显示项目列表的一部分(分页). 我的模板: div class="notif" *ngFor="let notif of paginate() | async" {{notif.name}}/div 在我的组件中如果我这样做: public notifications: ObservableNotification[];public paginate(): ObservableNotification[
我尝试显示项目列表的一部分(分页).
我的模板: <div class="notif" *ngFor="let notif of paginate() | async"> {{notif.name}} </div> 在我的组件中如果我这样做: public notifications: Observable<Notification[]>; public paginate(): Observable<Notification[]> { return this.notifications; } 这是有效的,但如果我这样做: public notifications: Observable<Notification[]>; public paginate(): Observable<Notification[]> { return this.notifications.map((notif: Notification[]) => { return notif; }); 它不再起作用了(我已经简化了功能以了解发生了什么). 解决方法
这是错误的,因为您尝试在返回Observable of Notification []的函数中返回Notification [],因此无效.
如果您需要Notification [],那么您需要订阅Observable. 您无法将Observable映射到其他类型. 例如 public paginate(): Observable<Notification[]> { return this.notifications; }); notificationArr: Notification[]; // This needs to be inside a method not in the class declaration of variables and methods paginate().subscribe((notif: Notification[]) => { return (this.notificationArr = notif); }); // should populate your array (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |