在当前的 Angular 6 应用程序中,有一个订阅 observable(来自 RESTful 服务的响应)
this.activatedRoute.data.subscribe(({bag}) => {
console.log(bag);
this.bag= bag;
});
订阅正在等待解析器的响应
@Injectable({
providedIn: 'root'
})
export class BagResolver implements Resolve<Bag> {
constructor(private service: BagService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
const existingBag = this.service.find(id).pipe(map((bag: HttpResponse<Bag>) => bag.body));
return existingBag;
}
return of(new Bag());
}
}
运行调试模式我发现 bag 是一个具有以下结构的对象
Object
{headers:,
body{id:10, name:aName, ...}, <-- this is the actual bag object
statusCode:"OK",
statusCodeValue:"200"
}
So bag
订阅中的内容并不是真正的 bag 对象,而是看起来像一个将 bag 对象包装在其主体中的 http 响应
现在我真的想提取body
从上面的对象并转换为本地对象
那么如何安全地提取数据并从上面的 subscribe(...) 返回中转换对象呢?