您的方法类型为void,因为您没有返回任何值。
那么你必须这样改变它。
private getProduct(id:string) {
this.dataService.getProduct(id).subscribe(product => {
this.product = product;
this.currentImage = product.image[0];
console.log('product = ', this.product);
});
return of(this.product);
}
如果您像这样重构代码,它的可读性会更高。
product$: Observable<any>;
ngOnInit() {
this.product$ = this.route.paramMap
.switchMap((params: ParamMap) =>
this.getProduct(params.get('id')));
this.product$.subscribe(product => {
this.currentImage = product.image[0];
});
}
private getProduct(id:string) {
return this.dataService.getProduct(id);
}