简体   繁体   English

在angular2中父母和子孙组件之间的数据通信(由路由器插座分开)?

[英]Data communication between parent and grand-child components (separated by a router outlet) in angular2?

[SOLVED] - @günter-zöchbauer [求助] - @günter-zöchbauer

I am new to Angular2 and TypeScript. 我是Angular2和TypeScript的新手。 I am trying to establish communication between a parent and a grand-child node. 我正在尝试在父节点和子节点之间建立通信。 The hierarchy in my app is as follows 我的应用程序中的层次结构如下

  • Parent
    • router-outlet 路由器出口
      • child1 child1
      • child2 的child2
      • child3 child3

I want to establish communication between child1 and parent. 我想在child1和parent之间建立通信。 I have created a data service (debug.service.ts) in the following manner:- 我以下列方式创建了一个数据服务(debug.service.ts): -

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { AngularFire, FirebaseListObservable } from 'angularfire2';


@Injectable()
export class DebugService {
    // Observable sources
    private _projectListSource = new Subject<FirebaseListObservable<any[]>>();

    // Observable streams
    projectListRetreived$ = this._projectListSource.asObservable();

    // Service message commands
    announceProjectsList(projects: any) {
        this._projectListSource.next(projects);
        console.log("Received   " + projects);
    }

}

The child component updates data in the following manner:- 子组件以下列方式更新数据: -

import { Component, ... } from '@angular/core';
import { DebugService } from './debug.service';
. . .

@Component({
  selector: 'my-projects',
  templateUrl: 'projects.component.html',
  styleUrls: ['projects.component.css'],
  directives: [
    ProjectDetailComponent,
    . . .
  ],
  providers: [
    DebugService,
    . . .
  ]
})

export class ProjectsComponent implements OnInit {
  projects: Observable<any[]>; 
  . . .

  // constructor
  constructor(
    private router: Router,
    . . .
    private debugService: DebugService) {

  }

  // gotoDashboardView method. CALLED at the click event of the dashboard floating action button on the template.
  gotoDashboardView() {
    this.router.navigate(['']);
  }

  // Supporting method for the ngOnInit() operation. CALLED by ngOnInit.
  getProjects() {
    this.projects = this.projectService.getProjects();
    this.debugService.announceProjectsList(this.projectService.getProjects());

  }

  // ngOnInit() method triggered at the initialization lifecycle hook. PROVIDED by angular2.
  ngOnInit() {
    this.getProjects();
  }

  deleteProject(key: string) {
    this.projectService.deleteProject(key);
  }
}

My parent component which contains the router outlet element in its template is as follows:- 我的父组件在其模板中包含路由器插座元素如下: -

import { Component, ... } from '@angular/core';
.
.
.
import { DebugService } from './debug.service';
import { Observable, Subscription } from 'rxjs';

@Component({
    moduleId: module.id,
    selector: 'app-root',
    templateUrl: 'app.component.html',
    styleUrls: [
        'app.component.css'],
    directives: [
        ProjectsComponent,
        ROUTER_DIRECTIVES,
        . . .
    ],
    providers: [
        DebugService,
        . . .
    ]
})

export class AppComponent implements OnDestroy, OnInit{
    projectObject: Observable<any[]>;
    subscription: Subscription;

    constructor(
        public af: AngularFire,
        private debugService: DebugService){ }

    clicky(){
        console.log(this.projectObject);
    }

    ngOnDestroy() {
        // prevent memory leak when component destroyed
        this.subscription.unsubscribe();
    }

    ngOnInit() {
        this.debugService.projectListRetreived$.subscribe(
            res => {
                this.projectObject = res;
            }
        );        
    }

}

As far as I can observe, the DebugService is receiving data, but for some reason the parent component is not being able to subscribe to it. 据我所知,DebugService正在接收数据,但由于某种原因,父组件无法订阅它。 Any ideas/suggestions as to how I can get router-outlet components to communicate with the parent component? 关于如何让路由器插座组件与父组件通信的任何想法/建议?

Provide DebugService only once at a common parent (can be the ancestor component if the components you want to communicate with are in a ancestor/descendant relationship). 仅在公共父级提供一次 DebugService (如果要与之通信的组件处于祖先/后代关系中,则可以是祖先组件)。

A new instance is created fro each provide. 为每个提供创建一个新实例。 If you provide on every component, every component will get its own service instance. 如果您在每个组件上提供,则每个组件都将获得自己的服务实例。 If you provide only once, this component and all descendants will get the same instance. 如果只提供一次,则此组件和所有后代将获得相同的实例。

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

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