Зачем запускать наблюдаемый субциб компонент parenet, когда дочерний компонент вызывает наблюдаемый субциб (они используют один и тот же сервис) - PullRequest
0 голосов
/ 29 марта 2019

Проблема в том, что когда я подписываю некоторые из наблюдаемых в 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-запроса иreturn observable

как я могу это исправить, чтобы подписка в KnowledgeGraphMainComponent не вызывалась, пока дочерний компонент подписывался

...