How to use home component data in other component?

爷,独闯天下 提交于 2019-12-29 08:26:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!