简体   繁体   English

通过内部的http.get可以观察到

[英]Observable with an http.get inside

I'm starting with angular. 我从角度开始。

I'm trying to create an Observable to load and put in cache data. 我正在尝试创建一个Observable来加载并放入缓存数据。 I whant to subscribe this observable from the HomeComponent off my app, and when the observable ping it because it's finish, I will appear buttons 我希望从我的应用程序的HomeComponent上预订此可观察的对象,当可观察的对象因为它完成而对其执行ping操作时,我将出现按钮

But, I have a problem I have a http.get to get the data from function : charger_data() Then if I succeed to have the data, I will transform it and put it in cache 但是,我有一个问题,我有一个http.get可以从函数中获取数据:charger_data()然后,如果我成功获取了数据,我将对其进行转换并将其放入缓存中

I whant my main function : charger_data_et_cache() return an observable 我想我的主要功能:charger_data_et_cache()返回一个可观察到的

I don't manage to do that, could you help me please !!! 我没有做到这一点,请你能帮我! ?

Thanks 谢谢

  public charger_data_et_cache(force:boolean) : Observable<boolean>{ this.charger_data().subscribe( (value) => { console.log('Observable change ! => ' + value); charger_cache(value); }, (error)=> { console.log('Observable error!'); }, () => { console.log('Observable complete!'); } ) return Observable.create( observer => { }); } public charger_data():Observable<string>{ return this.http.get('/assets/txt/mystere.txt',{responseType: 'text'}).pipe( map(data => { return data; }),catchError( error => { return throwError( 'error : ' + error ) }) ); } public charger_cache(data:string){ //do stuff } 

I have found ! 我已经发现 !

public charger_data_et_cache(force:boolean) : Observable<boolean>{

    if(force == false && MystereService.mystere_charge){
      return Observable.create((observer) => observer.complete());
    }
    return this.charger_data(force).pipe(map(data => {
        this.charger_cache(data);
        console.log('charger_cache ok');
        return true;
    }),catchError( error => {
        return throwError( 'error' )
      }));

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM