0

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

Mohit Kumar
  • 552
  • 9
  • 29

2 Answers2

1

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);
                }
            });
        }}
    }
raaaay
  • 496
  • 7
  • 14
Meadow
  • 317
  • 1
  • 6
-1

Use the below code.

constructor(private route: ActivatedRoute) { }
ngOnInit() {
    this.route.queryParams.forEach((params: Params) => {
      console.log(params['givenKey']);

      })
    });
  }
surendra kumar
  • 1,686
  • 11
  • 15
  • but I am not using any key const routes: Routes = [ { path: '', component: NavbarComponent },//this is the main router opens when init shown by router-outlet { path: 'login', component: NavbarComponent }, { path: 'signin', component: NavbarComponent }, { path: '**', component: PageNotFoundComponent }, ]; – Mohit Kumar Jul 20 '20 at 09:27
  • this is my route config – Mohit Kumar Jul 20 '20 at 09:28
  • what do you want using activated route? – surendra kumar Jul 20 '20 at 09:28
  • Actually I am using a dialog which is triggered by a button (name login) but when i add url manually Dialog doesn't opens – Mohit Kumar Jul 20 '20 at 09:30