简体   繁体   English

组件之间无法通信(Angular 2)

[英]Can't communicate between components (Angular 2)

I have a scenario where I need to pass data between two components Please find the below scenario of component tree. 我有一个需要在两个组件之间传递数据的方案。请找到下面的组件树方案。

     A
    / \
   B   C
  / \ / \
 D  E F  G

Here I need to pass data from G component to B component which are on same screen 在这里,我需要将数据从G组件传递到同一屏幕上的B组件

Service 服务

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class myCommService {
  private activeTabIndex = new Subject<number>();
  public activeTabIndexs = new Subject<number>();
  activeTab$ = this.activeTabIndex.asObservable();
  selectedCount$=this.activeTabIndexs.asObservable();
  constructor() { }
  fnActiveTabIs(astronaut: number) {
    debugger
    this.activeTabIndex.next(astronaut);
  }
  fnSelected(astronauts: number) {
    debugger
    this.activeTabIndexs.next(astronauts);
  }
}

G component: G成分:

import { Component, OnInit, Input } from '@angular/core';
import { myCommService} from '../../../../shared/services/comm.service';
@Component({
  selector: 'app-bl-view-widget-menu',
  templateUrl: './bl-view-widget-menu.component.html',
  styleUrls: ['./bl-view-widget-menu.component.css'],
  inputs: ['id'],
  providers: [myCommService]
})
export class BlViewWidgetMenuComponent implements OnInit {

  isToggle: boolean = true;
  i = 1;
  constructor(private myCommService: myCommService) { }
  id: Number;
  ngOnInit() {

  }

  fn(data, id) {
    debugger
    this.i = this.i + 1;
    // alert(this.i);
    this.myCommService.fnSelected(1);
  }

}

B component:" B组件:”

import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { myCommService} from '../../shared/services/comm.service';

@Component({
  // moduleId: module.id,
  selector: 'app-bl-filter',
  templateUrl: 'bl-filter.component.html',
  styleUrls: ['bl-filter.component.css'],
  providers: [myCommService]
})
export class BlFilterComponent implements OnInit {
  showInfo: boolean = true;
  activeTabIndex;
  a;
  constructor(
    private myCommService: myCommService
  ) {
    debugger
     myCommService.selectedCount$.subscribe(
       astronauts => {
         this.a = astronauts;
       });


  }

  ngOnInit() {
  }


}

but here I was able to update date in service but in B component I wan't able to get that data from service. 但是在这里我可以更新服务中的日期,但是在B组件中我无法从服务中获取该数据。 There is no error, in fact I could not debug. 没有错误,实际上我无法调试。 Any help would be appreciated. 任何帮助,将不胜感激。

It's because services are singleton per provider 这是因为每个提供商的服务都是单身

Since you are injecting different instances of the service to both G and B components, you won't be able to archieve this. 由于您要向G和B组件注入服务的不同实例,因此您将无法对此进行归档。

You need to provide service in a common module like 您需要在通用模块中提供服务,例如

@NgModule({
    imports: [],
    exports: [],
    declarations: [Gcomponent, BComponent], // <-- components declared in same module
    providers: [myCommService], // <--- service provided here
})

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

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