You may use canDeactivate guard when defining route, this will help you writing logic when changing route.
You may simply return true or false, based upon if you want to continue or reject route navigation, you also get reference to your component instance which is active for that route so as to read any property on it.
Signature of the method is like below,
canDeactivate(component: T,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) :
Observable<boolean>|Promise<boolean>|boolean
You may read more about it here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…