代码之家  ›  专栏  ›  技术社区  ›  sHamann

来自组件的Angular 2服务调用方法

  •  1
  • sHamann  · 技术社区  · 9 年前

    甚至可以让服务调用组件方法吗?

    myapp。组成部分

    export class MyAppComponent {
      public value;
      ...
      public setValue(payload){
        this.value = payload;
      }
    }
    

    myapp。服务

    @Injectable()
    export class MyAppService {
      private myAppComponent: MyAppComponent;
      private apiClientService: ApiClientService
    
      // ...
        After i make an PUT http call, the body from the response is my new "value"
      // ...
    
        putValue(payload: JSON){
    
        return this.apiClientService.putAPIObject(payload).then((response) => {
          this.myAppComponent.setValue(response);
        }); 
      }
    }
    

    这会导致 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'setValue' of undefined

    有人能解释一下我做错了什么吗? 提前谢谢。

    编辑: 既然人们抱怨我的方法,如果有人能告诉我什么是处理这个问题的最佳方式,我完全可以从头开始。

    我从api中获取值,更改它们,然后将它们放回api。我不想再次进行get调用,因此我在Put调用的响应中获得了我想要的新数据。

    调用来自组件-->组件服务-->apiclient服务

    我想问题是我在起点和终点之间有一个额外的服务。

    编辑2 :我试图避免 component service 让它只对我有用 component --> apiclient service

    就连这个解决方案目前也在为我工作,我有点不喜欢它,因为我必须从我的api中复制和粘贴许多代码,用于与其他对象相同的“操作”。例如,我使它适用于图片组件,但我也需要它用于我的电影组件。通常情况下,如果我在项目中经常编写相同的代码,这是一件坏事,还是没有?

    1 回复  |  直到 9 年前
        1
  •  4
  •   stealththeninja Jonathan Tuliani - MSFT    9 年前

    至少有两种方法可以解决这个问题,但希望这能给你一个开始。接受反馈和纠正。

    使用可观察的

    让服务拥有价值更改的知识并发出更改。组件侦听 上的事件发射器 1. Creating and returning Observable from Angular 2 Service )

    MyAppService

    import { Subject } from 'rxjs/Subject';
    
    @Injectable()
    export class MyAppService {
      private valueSource = new Subject<any>();
      public valueUpdate$ = this.valueSource.asObservable();
    
      putValue(payload: JSON){
        return this.apiClientService.putAPIObject(payload).then((response) => {
          /** here **/
          this.valueUpdate$.next(response);
        }); 
      }
    }
    

    MyAppComponent

    export class MyAppComponent {
      public value;
      private valueSubscription;
    
      constructor(private _myAppService: MyAppService) {}
    
      ngOnInit() {
        /** and here **/
        this._myAppService.valueUpdate$.subscribe((p) => this.setValue(p));
      }
      ...
      public setValue(payload){
        this.value = payload;
      }
    }
    

    注册组件

    回答最初的问题,想法是在服务中注册组件,以便它可以根据需要调用组件。您可以通过依赖项注入提取引用,但不建议这样做(例如,如果您的原始组件引用被破坏了怎么办?)

    MyAppService

    @Injectable()
    export class MyAppService {
      private myAppComponent: MyAppComponent;
    
      /** here **/
      registerMyApp(myApp: MyAppComponent) {
        this.myAppComponent = myApp;
      }
    
      putValue(payload: JSON){
        return this.apiClientService.putAPIObject(payload).then((response) => {
          this.myAppComponent.setValue(response);
        }); 
      }
    }
    

    MyAppComponent

    export class MyAppComponent {
      public value;
    
      /** and here **/
      constructor(myAppService: MyAppService) {
        myAppService.registerMyApp(this);
      }
      ...
      public setValue(payload){
        this.value = payload;
      }
    }
    
    1. 感谢AJT_82指出Angular不希望开发者在服务上使用EventEmitter: What is the proper use of an EventEmitter? .
    推荐文章