yes, you should be using canDeactivate
route guard.
Create a injectable service
@Injectable()
class CanDeactivateService implements CanDeactivate<TeamComponent> {
canDeactivate(
component: TeamComponent,
currentRoute: ActivatedRouteSnapshot,
currentState: RouterStateSnapshot,
nextState: RouterStateSnapshot
): Observable<boolean>|Promise<boolean>|boolean {
return component.isDirty ;
}
}
This can be used to determine if the page can be destroyed or not.
This should be configured in Routes as
RouterModule.forRoot([
{
path: '..', // path
component: Comp, // name of the component
canDeactivate: [CanDeactivateService]
}
])
],
Read more here...
Angular's Official Demo
This can be achieved through dynamic component loading also. Follow steps here