Angular4: how to pass data to another component using router navigate?

Viewed 6391

I'm developing an Angular2 app and I would like to navigate from a component to another one passing some data. In my case I need to pass a configuration object that I use in the component that I navigate to for changing some behaviours according to that configuration object.

private goToOtherComponent(): void {
    let options = {
        checkboxes: true,
        actions: ['save', 'close'],
        items: ['foo', 'bar'],
        clickable: false
    };

    this.route.navigate(['documents'], options)
        .then(response => {
            console.log('response', response);
        })
        .catch(error => {
            console.log('error', error);
        });
}

I have read some questions like mine and I have read Angular documentation, but I can't figure out how to do that.

  • RouteParams is deprecated
  • queryParams is not what I'm looking for
  • RouteSegment I think is not suitable for my case
  • Using a shared service is a solution I do not like

Here is how I pass data in the routing ngModule file:

{path: 'documents', data: { someValue: 'foo', otherValue: 'bar' }

I would like to have a similar way to pass data using navigate method. Then how to retrieve that options object in the component that I navigate to?

0 Answers
Related