如何在其他组件中使用家庭组件数据? [英] How to use home component data in other component?

查看:126
本文介绍了如何在其他组件中使用家庭组件数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码,如何在其他组件中使用(this.categoryType)

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

以上代码我在webservice.ts中使用

Above code I am using in webservice.ts

ngOnInit() { this.wsp.getCategoryDetails().subscribe(data => {
this.categoryType = data; });
}

以上代码我在home.ts中使用过,但我想用这个回复在其他组件中。

Above code I have used in home.ts, but I want use this response in other component.

推荐答案

使用服务保存类别。然后你可以使用相同的服务来获得categoryType。

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

这篇关于如何在其他组件中使用家庭组件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆