Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass data from child to parent inside of router-outlet

Tags:

angular

I have a parent component with a router-outlet. I want to call the parent function with a child event. I understand @Input() and @Output, but how do you use them with router-outlet

<router-outlet (tapVocab)="openVocabModal($event)"></router-outlet>

Doesn't seem to work for me. In my child I have:

@Output() tapVocab = new EventEmitter<string>();

callParent(val) {
    console.log('calling parent', val);
    this.tapVocab.next(val);
  }
like image 318
bradrice Avatar asked Sep 10 '19 21:09

bradrice


People also ask

Can we pass data in router outlet?

Fortunately page-router-outlet component same as its ancestor router-outlet has activate event emitter which provides us with the currently activated element. So we can use this information in order to pass data to the activated child component or subscribe to one's events in an imperative way.

How do I transfer data to my child's route?

Inputs are the simplest and most direct way of passing data to a child component. Simply adding the Input decorator to a property will allow it to be passed in. As you probably know by now, inputs accept data via properties. You can pass methods, constants, or any expression into an input.


1 Answers

From angular

<router-outlet> Acts as a placeholder that Angular dynamically fills based on the current router state.

<router-outlet> does not offer a means to bind data to loaded components or emit events from them to parents.

But it has two events:

activate — emits any time a new component is instantiated.

deactivate — emits when the component is destroyed.

<router-outlet (activate)="componentAdded($event)" (deactivate)="componentRemoved($event)"></router-outlet>

But I don't think it will be useful for your case.

But you can communicate like any other communication between two unrelated components using service.

common.service.ts:

@Injectable()
export class CommonService {
  private data = new BehaviorSubject('default data');
  data$ = this.data.asObservable();

  changeData(data: string) {
    this.data.next(data)
  }
}

app.component.ts:

@Component({
  selector: 'app-component',
  template: `<p>{{data}}</p>`
})
export class AppComponent implements OnInit {

  data: string;

  constructor(private service: CommonService) { }

  ngOnInit() {
    this.service.data$.subscribe(res => this.data = res)  //read the invoked data or default data
  }

}

child.component.ts:

@Component({
  selector: 'app-component-two',
  template: `
    <p>{{data}}</p>
    <button (click)="newData()">Next Data</button>`
})
export class ComponentTwoComponent implements OnInit {

  data: string;

  constructor(private service: CommonService) { }

  ngOnInit() {
    this.service.data$.subscribe(res => this.data = res)
  }
  newData() {
    this.service.changeData('Changed Data');  //invoke new Data
  }
}
like image 188
Md Rafee Avatar answered Oct 19 '22 08:10

Md Rafee