如何在其他组件中使用家庭组件数据?
问题描述:
下面是我在其他组件代码,如何使用(this.categoryType)如何在其他组件中使用家庭组件数据?
getCategoryDetails(){
return this.http.get('ip/ramu/api/api/…')
.map((res:Response) => res.json());
}
上面的代码。我正在使用webservice.ts
ngOnInit() { this.wsp.getCategoryDetails().subscribe(data => {
this.categoryType = data; });
}
上面的代码,我在home.ts使用,但我想在其他组件中使用此响应。
答
使用服务保存类别。然后,您可以使用相同的服务在需要时获取categoryType。
import { Injectable } from '@angular/core';
@Injectable()
export class AppMessageQueuService {
categoryType: string;
constructor() {
}
saveCateogry(cat:any){
this.categoryType= cat;
}
retrieveCategory(){
return this.categoryType;
}
}
然后你可以注入本服务到两个组件,它从第一部件一样保存,
this.appMsgService.saveCateogry(yourcategory);
然后在第二个组件作为检索,
this.appMsgService.retrieveCategory();
服务工作像魅力 –