2017-07-08 96 views
0

下面是我在其他組件代碼,如何使用(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使用,但我想在其他組件中使用此響應。

回答

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

服務工作像魅力 –

相關問題