问题
Below is my code, how to use (this.categoryType) in other component
getCategoryDetails(){
return this.http.get('ip/ramu/api/api/…')
.map((res:Response) => res.json());
}
Above code I am using in webservice.ts
ngOnInit() { this.wsp.getCategoryDetails().subscribe(data => {
this.categoryType = data; });
}
Above code I have used in home.ts, but I want use this response in other component.
回答1:
Use a service to save the category. And then you can use the same service to get the categoryType whenever you want it.
import { Injectable } from '@angular/core';
@Injectable()
export class AppMessageQueuService {
categoryType: string;
constructor() {
}
saveCateogry(cat:any){
this.categoryType= cat;
}
retrieveCategory(){
return this.categoryType;
}
}
Then you can inject this Service into both the components and save it from the first component like,
this.appMsgService.saveCateogry(yourcategory);
then retrieve in the second component as,
this.appMsgService.retrieveCategory();
来源:https://stackoverflow.com/questions/44984240/how-to-use-home-component-data-in-other-component