【问题标题】:How do I return the Resolved Data with the new RXJS 6 pipe/map in Angular 6? [closed]如何在 Angular 6 中使用新的 RXJS 6 管道/映射返回已解析的数据? [关闭]
【发布时间】:2018-12-31 05:14:22
【问题描述】:

如何在 Angular 6 中使用新的 RXJS 6 管道/映射返回已解析的数据?

@Injectable()
export class RecentDataResolver implements Resolve<RecentData> {
    constructor(
        public data: DataService,
        private api: ApiService) {}

    resolve(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
      ): Observable<any> {

        this.api.get('/data/recent').pipe(map(response => {
            return response as RecentData;
        }));
      }
}

我在组件中订阅时解析的数据是null

constructor(private route: ActivatedRoute, public data: DataService) {

    this.route.data.subscribe(routeData => {
      this.recent = routeData.recent;
      console.log('data', routeData.recent);
    });

}

【问题讨论】:

    标签: javascript angular api rxjs angular6


    【解决方案1】:

    您必须从解析器函数返回一个可观察对象。

    您错过了退货声明。

    return this.api.get('/data/recent').pipe(map(response => {
         return response as RecentData;
    }));
    

    【讨论】:

    • 天哪,这是一个菜鸟错误:P 谢谢,它有效!
    【解决方案2】:

    如果您没有在 get() 中执行任何逻辑,您可以简单地将其返回为

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {    
            return this.api.get<RecentData>('/data/recent');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2019-02-28
      • 2018-11-18
      • 2018-11-03
      • 1970-01-01
      相关资源
      最近更新 更多