Get target route URL in CanActivate guard Lazy load modules Angular

Asad Shah

I'm trying to get the target URL in canActivate guard but unable to do so. I've configured preloadingStrategy: PreloadAllModules in RouterModule.forRoot when i use ActivatedRoute its url property doesn't contain path. here's both of my module:

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 need activated route in RouteGuard.

ForestG
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    console.log("route-access", state);
}

from this answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can't get route params for a lazy module within a CanActivate guard

Angular(5) - lazy loading and canActivate guard - StaticInjectorError

Angular Route Guard CanActivate stops routing

Read cookie in CanActivate route guard in Angular

Router infinite loop with second canActivate guard on lazy-loaded modules

How to redirect from parent route to child route in Angular canActivate guard?

Angular 2 Route Guard CanActivate based on Firebase User Role

Angular Router route guard CanActivate always returns false

Angular canActivate Guard with API

Angular CanActivate Guard

Angular - Lazy load conditional route

Angular How to implement CanActivate guard with HTTP Get call

How can Vue router get current route path of lazy-loaded modules on page load?

Angular 8 - How to load lazy modules into tabs

Angular lazy load with parent route parameter

Angular CanLoad guard triggers only once upon first lazy load?

Angular 2 get current route in guard

angular load modules based on url

How to load modules on a route in Angular 8?

Angular5 canactivate guard :Types of property 'canActivate' are incompatible

Angular CanActivate guard - createUrlTree relative navigation

Angular2 CanActivate guard not working

Using defaultIfEmpty() still not emitting in Angular canActivate guard

Angular 2 router canActivate Auth Guard

Authorization Issue with Angular Auth Guard canActivate method

Unable to get route working with 2 levels of lazy loaded modules (nested)

How to get requesting url in guard angular 2

how to build angular 7 project with lazy load modules

How to use common directive for lazy load modules in angular 8