简体   繁体   English

如何在其他组件中使用家庭组件数据?

[英]How to use home component data in other component?

Below is my code, how to use (this.categoryType) in other component 下面是我的代码,如何在其他组件中使用(this.categoryType)

getCategoryDetails(){
return this.http.get('ip/ramu/api/api/…')
.map((res:Response) => res.json());
}

Above code I am using in webservice.ts 以上我在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. 以上我在home.ts中使用的代码,但是我想在其他组件中使用此响应。

Use a service to save the category. 使用服务保存类别。 And then you can use the same service to get the categoryType whenever you want it. 然后,您可以随时使用同一服务获取categoryType。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM