It is recommended not to pass data objects through routes, but rather utilize a common service or BehaviorSubject(rxjs) for such purposes.
If you still wish to pass data through routes, here is how you can do it:
In the current component, set the data to the route as shown below
import { Component } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
@Component({
selector: 'app-form1',
templateUrl: './form1.component.html',
styleUrls: ['./form1.component.css']
})
export class Form1Component {
constructor(private router: Router) {
}
handleButtonClick() {
const myObject = { name: 'John', age: 30 };
const navigationExtras: NavigationExtras = {
state: {
data: myObject
}
};
this.router.navigate(['/list'], navigationExtras);
}
}
Next, access the data in the target component within the ngOnInit
method
ngOnInit() {
const currentState = this.router.lastSuccessfulNavigation;
console.log(currentState?.extras)
}