I am working on an angular application and I need to implement a feature where I can detect when a user navigates outside of the app domain from a specific component.
For instance, let's say the user is on the upload component processing important information and decides to navigate away using methods like router, window.location.href, window.location.assign, manually entering a URL, or clicking a link in an advertisement. I want to be able to capture this event in Angular and based on certain conditions, allow or prevent the navigation.
I am aware that we can use the router to detect route changes within the Angular app, but it doesn't cover scenarios like external routing through window.location or from ad popups. Below is the code snippet I tried using the router approach:
this.router.events.forEach((e: any) => {
if (e instanceof NavigationStart) {
console.log('routing to ' + JSON.stringify(e));
}
});
In essence, my goal is to block all external navigations from one specific component in the app while still allowing internal app routing. Is there a way to achieve this?