1

I have a problem regarding angular routing not loading my component the first time.

Example: When i write "http://localhost:4200" i getting redirected to "http://localhost:4200/ENU" which is correct, but then my login component is not loaded correctly, im just getting a blank website with the url of "http://localhost:4200/ENU". And then when i refresh my browser,the login component is loaded correctly.

this is my routing setup in angular:

const routes: Routes = [
  {path: '', redirectTo: 'ENU', pathMatch: 'full'},
  {path: ':language', component: LoginComponent},
  {path: ':language/home', component: HomeComponent, canActivate: [AuthGuard]},
  {path: ':language/trucks', component: TrucksComponent, canActivate: [AuthGuard]},
  {path: '**', redirectTo: 'ENU', pathMatch: 'full'},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}

This is Angular v16

Any suggestions?

I want to load my login component when i write "http://localhost:4200" and then getting redirected to "http://localhost:4200/ENU"

this is my auth-guard:

    export class AuthGuard {
  private _authService: AuthService = inject(AuthService);
  private _router: Router = inject(Router);

  async canActivate(): Promise<| Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree> {
    if (!this._authService.isLoggedIn) {
      await this._router.navigate(['/ENU']);
    }
    return true;
  }
}
4
  • please share the code for AuthGuard Commented Apr 25, 2024 at 9:39
  • i have added it in my edited version @NarenMurali Commented Apr 25, 2024 at 10:23
  • unable to replicate your issue stackblitz please replicate and share back! Commented Apr 25, 2024 at 10:47
  • 1
    Sorry for wasting your time, i just found out that i have this ngif "*ngIf="(translationService.translationsSub$ | async)?.size! > 0"" in my html and thats why i was getting a blank website. I appreciate your time @NarenMurali <3 Commented Apr 25, 2024 at 11:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.