Someguy
Someguy

Reputation: 414

skipLocationChange On Routes Array. Angular 11

Im Curious how you skipLocationChange on the Routing Module array.

onst routes: Routes = [
  { path: '', redirectTo: '/login', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'register', component: RegisterComponent },
  { path: 'dashboard', component: DashboardComponent },
  { path: 'createentry', component: CreateentryComponent },
  { path: 'dateview', component: DateviewComponent },
  {path: 'profile', component: ProfileComponent},
  { path: 'calendar', component: CalendarComponent, resolve: { singlePost: SinglePostResolver}}
 

];

I automatically redirect to the login at the start, But how do I skip the Url append in here?

Upvotes: 0

Views: 1157

Answers (1)

Andrei
Andrei

Reputation: 12196

there is no possibility to navigate without location change in router config. you can probably make a guard which will be activated on the path navigation and will redirect everything that comes

const routes = [
 { path: '', canActivate: [RedirectGuard], data: {redirectTo: '/login'}, pathMatch: 'full' },
...
]
export class RedirectGuard {
  constructor(private router: Router) {}
  canActivate(routeSnapshot) {
    this.router.navigateByUrl(routeSnapshot.data.redirectTo, {skipLocationChange: true});
  }
}

Upvotes: 1

Related Questions