VIK6Galado
VIK6Galado

Reputation: 680

Router navigate is not working in Angular

I have setup routing in my angular project and below are my routes in root module

const appRoutes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'dashboard', canActivate: [AuthGuard], component: DashboardComponent }
];

In my app.component.ts i have places my <router-outlet></router-outlet>.

In my login page I have a function which navigates to http://localhost:4200/dashboard once successful logging in

login.component.ts

login() {
....
this.router.navigate(['dashboard'], {relativeTo: this.route});
....
}

Now the problem here is when I do ng serve and open localhost:4200 in the browser, I will not be able to login. Only after reloading the page i.e., localhost:4200 I will be able to login.

What is stopping me to login here without reloading? please let me know :)

Upvotes: 1

Views: 1447

Answers (1)

Fadhilah Rahman
Fadhilah Rahman

Reputation: 36

I think when you try to navigate to route 'dashboard' the guard of routing rejects your action because maybe not a valid token or some logic in AuthGuard. You must check your AuthGuard

Upvotes: 2

Related Questions