In one of my Angular 2 routes\'s templates (FirstComponent) I have a button
first.component.html
Angular 7.2.0 introduced new way of passing the data when navigating between routed components:
@Component({
template: `Go`,
})
export class AppComponent {
constructor(public router: Router) {}
navigateWithState() {
this.router.navigateByUrl('/123', { state: { hello: 'world' } });
}
}
Or:
@Component({
selector: 'my-app',
template: `
Go`,
})
export class AppComponent {}
To read the state, you can access window.history.state
property after the navigation has finished:
export class PageComponent implements OnInit {
state$: Observable