Subscribe to observable is returning undefined

后端 未结 6 715
面向向阳花
面向向阳花 2020-12-01 07:27

So I am trying to subscribe to a simple service that return data from a local JSON file.

I have managed to get the service working, I can log it out in the function

6条回答
  •  醉酒成梦
    2020-12-01 07:44

    you can do it like this:

    In your app-component:

    public getDataFromService() {
      this._api.getData(this);
    }
    
    
    public setData(data: any){
     this.data=data;
    }
    

    In your service/api.ts:

    public getData(obj: appComponentModel){
        this.http.get(url).subscribe(res => obj.setData(res));
    }
    

提交回复
热议问题