为什么子组件调用可观察子(它们使用相同的服务)时触发parenet组件可观察子

时间:2019-03-29 05:51:10

标签: angular rxjs angular2-observables

问题是,当我在子组件中订阅了GraphService中的某些可观察项(即使它不是getProductGraph方法)时, console.log('It happen again!!!!!') 在父组件中订阅将成为我不希望的呼叫。

ngOninit和mergeMap KnowledgeGraphMainComponent不被调用,仅订阅代码被调用。

这是我的代码: 父组件:使用graphQLService初始化一些数据

export class KnowledgeGraphMainComponent implements OnInit {
  nodeTypes: [];

  constructor(
    private graphQLService: GraphQLService,
    public graph: GraphService,
  ) {
  }

  ngOnInit() {
    this.route.queryParams
      .pipe(
        mergeMap(queryParams => {
          const filterMap = this.initFilterMap(queryParams);
          this.filterContainer = new FilterContainer(filterMap);
          return this.graphQLService.getAllNodeFields();
        }),
        mergeMap(fields => {
          this.nodeTypes = fields['fields'];
          let params = this.filterContainer.getParams();
          params = this.sharedUtility.removeEmptyKey(params);
          return this.graphQLService.getProductGraph(params);
        })
      )
      .subscribe(result => {
        console.log('It happen again!!!!!');
        if (!this.graph.simulation || this.event !== '') {
          this.graph.graph = this.utility.convertApolloToGraphKA(result['data']['nodes']['products'], EntityName.product);
          this.graph.conditionModel.limit = this.filterContainer.filters['page'].limit;
          this.graph.conditionModel.offset = this.filterContainer.filters['page'].offset;
          if(!this.graph.simulation){
            this.graph.initgraph();
          }
          this.graph.updategraph();
          this.event = '';
        }
        this.spinner.hide();

      });
  }

}

子组件:使用GraphService并通过父组件传递

export class GraphD3ViewComponent implements OnInit {

  @Input() graph:GraphService;
  @Input() nodeTyps:[];

  constructor(
  ) {
  }

  ngOnInit() {
  }
}

GraphService需要使用GraphQLService来执行一些http请求

export class GraphService extends Graph {

    that: any;

    constructor(
        private graphQLService: GraphQLService,
    ) {
        super();
    }
}

GraphQLService是执行http请求并返回可观察的

我该如何解决在子组件订阅时不会调用KnowledgeGraphMainComponent中的订阅

0 个答案:

没有答案