0

Starting in angular, I am faced with the following difficulty: I have to pass a data from a child routed component to a parent component.

I thought I could use @Output with a eventEmitter. But I feel like it’s not possible I also tried to use a topic that generates a change detection error (an ExpressionChangedAfterItHasBeenCheckedError).

ts of child compo :

 ngOnInit(): void {
    this.itemsMenu = this.rayonService.itemsMenu;
    this.activatedRoute.paramMap.subscribe(params => {
      let paramUrlZone = params.get('id');

      if (paramUrlZone !== null) {
        this.isTest = true; // pass to the parent
      }
    });
    this.initRefForm();
  }

html parent (child compo is routed...)

------------------------------------
      <div class="router-container">
        <router-outlet></router-outlet>
      </div>
------------------------------------

I don't how to do the stuff.. Thanks for your help !

olivier
  • 139
  • 2
  • 10
  • please show what you have done so far. Maybe this helps you: https://stackoverflow.com/questions/67268840/cross-component-communication-using-emit-and-subscribe-not-working/67269104#67269104 – jo-chris Jul 28 '21 at 15:14
  • thanks for the link, I’ll go and see – olivier Jul 28 '21 at 15:35

1 Answers1

1

You can avoid passing id to the parent by directly subscribing to the child's params data in the parent component like this:

constructor(route: ActivatedRoute) {
  route.firstChild.params.subscribe((params) => {
    console.log(params.id);
  });
}
Logan Young
  • 385
  • 2
  • 9
  • ok, thanks for your answer, I’ll try, but I’m not sure it works to get the isTest variable. – olivier Jul 28 '21 at 20:18