当角度完成服务功能时通知组件

时间:2022-01-13 03:35:33

I writing a service which do a http query to the backend:

我编写了一个对后端执行http查询的服务:

getPlacesForUser(){
 this.http.get("http://localhost:8080/processPlaces")
  .map(res => res.text())
  .subscribe(
    data => this.result = data,
    err => this.logError(err),
    () => console.log('Places Request completed')
  )
}

Now I want to update the components-side, if the request is completed:

现在我想更新组件端,如果请求完成:

processPlaces(){
 this._backendService.getPlacesForUser();
}

How can I communicate between the component and service?

如何在组件和服务之间进行通信?

Furthermore I searching for the best practice.

此外,我寻找最佳实践。

2 个解决方案

#1


4  

In fact, you can return the observble directly from the getPlacesForUser method and attach a subscribe method on it.

实际上,您可以直接从getPlacesForUser方法返回observble并在其上附加一个subscribe方法。

  • Service

    服务

    getPlacesForUser(){
      return this.http.get("http://localhost:8080/processPlaces")
        .map(res => res.text());
    }
    
  • Component

    零件

    processPlaces(){
     this._backendService.getPlacesForUser()
      .subscribe(
        data => this.result = data,
        err => this.logError(err),
        () => console.log('Places Request completed')
      );
    }
    

The result attribute corresponds to an attribute of the component you can use in the component.

result属性对应于您可以在组件中使用的组件的属性。

You can notice that a async pipe is available to directly leverage observable in expressions (for example within an ngFor one).

您可以注意到异步管道可用于直接利用表达式中的observable(例如在ngFor中)。

I think that this answer could help you: How to Consume Http Component efficiently in a service in angular 2 beta?.

我认为这个答案可以帮到你:如何在角度2 beta的服务中有效地使用Http组件?

Hope it helps you, Thierry

希望它对你有帮助,蒂埃里

#2


0  

There is another way to call data each and every component with Rxjs framework, Make sure services should be reusable for all component, so do much as u can do code in services and subscribe only in component :

还有另一种方法可以使用Rxjs框架调用每个组件的数据,确保服务应该可以重用于所有组件,因此,您可以在服务中执行代码并仅在组件中进行订阅:

Services

服务

 import { Injectable } from 'angular2/core';    
 import { Http } from 'angular2/http';
 import { Result} from './models/result'; // model imported
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';

 @Injectable()
 export class Service {
   private _result: Result[];

   constructor(private http: Http) {
     this.result$ = new BehaviorSubject<Result[]>(this._result)
   }
   result$: BehaviorSubject<Result[]>;
   private _urlGet = '/api';

   getPlacesForUser(): void{
     this.http.get(this._urlGet)
      .map(res => JSON.parse(res.text()))
      .catch(error => console.log(error))
      .subscribe(result => {
         this.result = result;
       })
    }
 }

Component

零件

 constructor(private _service: Service)
 ngOnInit(): any {
   this._service.result$.map(result => result.filter(res => 
    res.id)).subscribe(
        result => this.result = result); // map is optional to filter json
  }

#1


4  

In fact, you can return the observble directly from the getPlacesForUser method and attach a subscribe method on it.

实际上,您可以直接从getPlacesForUser方法返回observble并在其上附加一个subscribe方法。

  • Service

    服务

    getPlacesForUser(){
      return this.http.get("http://localhost:8080/processPlaces")
        .map(res => res.text());
    }
    
  • Component

    零件

    processPlaces(){
     this._backendService.getPlacesForUser()
      .subscribe(
        data => this.result = data,
        err => this.logError(err),
        () => console.log('Places Request completed')
      );
    }
    

The result attribute corresponds to an attribute of the component you can use in the component.

result属性对应于您可以在组件中使用的组件的属性。

You can notice that a async pipe is available to directly leverage observable in expressions (for example within an ngFor one).

您可以注意到异步管道可用于直接利用表达式中的observable(例如在ngFor中)。

I think that this answer could help you: How to Consume Http Component efficiently in a service in angular 2 beta?.

我认为这个答案可以帮到你:如何在角度2 beta的服务中有效地使用Http组件?

Hope it helps you, Thierry

希望它对你有帮助,蒂埃里

#2


0  

There is another way to call data each and every component with Rxjs framework, Make sure services should be reusable for all component, so do much as u can do code in services and subscribe only in component :

还有另一种方法可以使用Rxjs框架调用每个组件的数据,确保服务应该可以重用于所有组件,因此,您可以在服务中执行代码并仅在组件中进行订阅:

Services

服务

 import { Injectable } from 'angular2/core';    
 import { Http } from 'angular2/http';
 import { Result} from './models/result'; // model imported
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';

 @Injectable()
 export class Service {
   private _result: Result[];

   constructor(private http: Http) {
     this.result$ = new BehaviorSubject<Result[]>(this._result)
   }
   result$: BehaviorSubject<Result[]>;
   private _urlGet = '/api';

   getPlacesForUser(): void{
     this.http.get(this._urlGet)
      .map(res => JSON.parse(res.text()))
      .catch(error => console.log(error))
      .subscribe(result => {
         this.result = result;
       })
    }
 }

Component

零件

 constructor(private _service: Service)
 ngOnInit(): any {
   this._service.result$.map(result => result.filter(res => 
    res.id)).subscribe(
        result => this.result = result); // map is optional to filter json
  }