Search code examples
angularroutesangular-ui-routerangular-activatedroute

How to get Activatedroute in angular


I want to get the route when the URL changes

https://localhost:3200

to

https://localhotst:3200/login

how can i get login when there is change in route I tried ActivateRoute but the code i made is not working

Here is what I've tried :

route.params.pipe(
  takeUntil(this.destroy)
).subscribe(params => {
  if (this.currentDialog) {
    this.currentDialog.close();
  }
  this.currentDialog = matDialog.open(DialogComponent, {
    data: { tabvalue, param.id}
  }); //this is the code i took from a web site but i don't know how to apply it
});

In param.id here id is a variable but i don't want to use it. I just want path: 'login', component: NavbarComponent } someting like this path


Solution

  • You can subscribe to route change via route event.

    To get changes globally, you can set the subscription in in your app.component.ts for example.

    Get only the NavigationEnd event, and then retrieve the desired url.

        class MyClass implements OnInit {
        
            constructor(private router: Router) {}
        
            ngOnInit() {
                this.router.events.subscribe((routerEvent) => {
                    if(routerEvent instanceof NavigationEnd) {
                        // Get your url
                        console.log(routerEvent.url);
                    }
                });
            }}
        }