My Пример .
На маршруте только один раз укажите компонент, чтобы избежать воссоздания:
{
path: 'data',
redirectTo: 'data/'
},
{
path: 'data/:id',
component: CreateAndEditComponent
}
А в компоненте следите за изменениями параметров:
ngOnInit() {
console.log('init');
const getParams = () => {
const dataId = this.route.snapshot.paramMap.get('id');
this.data = dataId.length > 0 ? this.getDataById(dataId) : new Data();
};
this.router.events.pipe(takeUntil(this._destroyed)).subscribe(event => {
// only listen for NavigationEnd as that indicates navigation processing is complete
if (event instanceof NavigationEnd) {
// get params for each consecutive route
getParams();
}
});
// get params for initial route
getParams();
}
save() {
this.service.saveData(this.data);
this.router.navigate(['/data', this.data.id]);
}