I am facing an issue with retrieving the target URL in the canActivate guard. Even though I have set up preloadingStrategy: PreloadAllModules in RouterModule.forRoot, the url property of ActivatedRoute does not contain the path. Here are the contents of both of my modules:
app.module.ts
const appRoutes: Routes = [
{ path: 'login', component: LoginComponent },
{
path: '',
component: SiteLayoutComponent,
children: [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
{
path: 'user',
loadChildren: './user/user.module#UserModule'
}
]
},
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
declarations: [
AppComponent,
SiteLayoutComponent,
LoginComponent,
PageNotFoundComponent,
],
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: false, preloadingStrategy: PreloadAllModules }
),
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
HttpModule,
FormsModule,
CommonModule,
],
providers: [AuthGuardAuthService, LoginService, SharedService],
bootstrap: [AppComponent]
})
export class AppModule { }
user.module.ts
const appRoutes: Routes = [
{
path: 'user',
children: [
{ path: '', redirectTo: 'create', pathMatch: 'full' },
{
path: 'create', component: CreateComponent, canActivate: [RouteGuard] //need activated route in this guard
},
{ path: ':id/edit', component: CreateComponent },
{ path: 'list', component: UserListComponent }
],
},
{
path: 'role',
children: [
{ path: '', redirectTo: 'create', pathMatch: 'full' },
{
path: 'create', component: RoleComponent,
resolve: {
Modules: RolesResolver
}
},
{
path: ':id/edit', component: RoleComponent,
resolve: {
Modules: RolesResolver,
}
},
{ path: 'list', component: RoleListComponent },
],
},
];
@NgModule({
declarations: [
CreateComponent,
RoleComponent,
UserListComponent,
RoleListComponent
],
imports: [
CommonModule,
FormsModule,
RouterModule.forChild(
appRoutes
)
],
providers: [RouteGuard, UserService, RoleService, RolesResolver],
})
export class UserModule { }
I require the activated route in RouteGuard.