【问题标题】:How to use home component data in other component?如何在其他组件中使用 home 组件数据?
【发布时间】:2017-12-12 13:07:12
【问题描述】:

下面是我的代码,如何在其他组件中使用(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 中使用过,但我想在其他组件中使用这个响应。

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

    使用服务来保存类别。然后,您可以随时使用相同的服务来获取 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();
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 2021-04-28
      • 2015-06-01
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多