Json 如何在组件之间共享我通过subscribe in组件获得的数据

Json 如何在组件之间共享我通过subscribe in组件获得的数据,json,angular,observable,subscribe,Json,Angular,Observable,Subscribe,在我的组件中,我使用.subscribe constructor(private CityWeatherDataService: CityWeatherDataService){} daysForecast(city,country){ this.CityWeatherDataService.daysForecast(city,country) .subscribe(data => { for (var i = 0; i < data.length

在我的组件中,我使用.subscribe

constructor(private CityWeatherDataService: CityWeatherDataService){}

  daysForecast(city,country){

    this.CityWeatherDataService.daysForecast(city,country)
    .subscribe(data => {
      for (var i = 0; i < data.length; i++) {
        this.humidity.push(data[i].humidity);
      } 
      return data;    
    });

  }

  ngOnInit() {
    this.daysForecast("London","UK");
    console.log(this.humidity);
  }
构造函数(私有城市天气数据服务:城市天气数据服务){
日期预测(城市、国家){
this.CityWeatherDataService.daysforcast(城市、国家)
.订阅(数据=>{
对于(变量i=0;i
我的服务我喜欢

daysForecast(city,country): Observable<WeatherItem[]>{
    const params = new HttpParams()
    .set('city', city)
    .set('country', country)
    .set('key', '7fddb2fc6bae42a396f121c7bd341832');
    return this.http.get('https://api.weatherbit.io/v2.0/forecast/daily', {params})
    .map(data=>{
      let forecastItemList = data["data"];
      return forecastItemList.map(function(forecastItem:any) {        
        return {            
          weatherDescription: forecastItem.weather.description,
          weatherIcon: forecastItem.weather.icon,
          temperature: forecastItem.temp,
          humidity: forecastItem.rh,
          wind: forecastItem.wind_spd,
          cloudiness: forecastItem.clouds,
          pressure: forecastItem.pres
        };
      });
    });
  } 
daysforcast(城市、国家):可观察{
const params=新的HttpParams()
.set('城市',城市)
.set('country',country)
.set('key','7fddb2fc6bae42a396f121c7bd34832');
返回此.http.get('https://api.weatherbit.io/v2.0/forecast/daily“,{params})
.map(数据=>{
设forecastItemList=data[“data”];
返回forecastItemList.map(函数(forecastItem:any){
返回{
weatherDescription:forecastItem.weather.description,
weatherIcon:forecastItem.weather.icon,
温度:预测温度,
湿度:forecastItem.rh,
风:forecastItem.wind\u spd,
云量:天气预报,
压力:预测压力
};
});
});
} 
是否可以使用来自的数据。不仅在此函数中订阅,还可以共享以在其他组件中使用?现在,当我从.subscribe共享数据时,我只能在子组件中使用ngFor显示它,但它不能在组件中使用此数据,但我需要这样做


谢谢大家!

AndrewGumenniy,观测值是异步的。这意味着,直到它没有完成,你没有数据。但是你不需要关心它。如果您的子组件具有@Input,则当数据更改时,日期将显示在子组件中。您只需要一个变量

@Component({
  selector: 'app-root',
  template: `
    <app-child [data]="data"></app-child>
  `
})
constructor(private CityWeatherDataService: CityWeatherDataService){}
  data:any[]; //<--your variable

  daysForecast(city,country){

    this.CityWeatherDataService.daysForecast(city,country)
    .subscribe(data => {
      this.data=data.map(x=>x.humidity); //<--an abreviate way to do your loop
      //you needn't return anything
    });

  }

  ngOnInit() {
    this.daysForecast("London","UK");
    console.log(this.humidity); //<--this give you "null", but that's not important
  }
@组件({
选择器:'应用程序根',
模板:`
`
})
构造函数(专用城市天气数据服务:城市天气数据服务){}
数据:任意[];//{

this.data=data.map(x=>x.湿度);//谢谢!是的,数据显示在ngFor的子模板中,但我不会在子组件中使用它,因此它是未定义的。如果要在订阅的同一组件中使用,请使用变量“data”,例如在应用程序根中写入{data}}。如果您想订阅一个组件并在另一个组件中显示,您可以查看