6

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.

Asad Shah
  • 771
  • 2
  • 8
  • 22

1 Answers1

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

from this answer.

ForestG
  • 17,538
  • 14
  • 52
  • 86
  • already tried it gives: StaticInjectorError[ActivatedRouteSnapshot]: NullInjectorError: No provider for ActivatedRouteSnapshot! – Asad Shah Mar 21 '18 at 11:41
  • do you import it like this? `import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from "@angular/router"; ` – ForestG Mar 21 '18 at 11:48
  • yes, here's my import for RouteGuard: import { Router, CanActivate, ActivatedRoute, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; – Asad Shah Mar 21 '18 at 11:50
  • Its really strange, couse this works for me. What Angular version are you using? Is this produced with Angular 5? – ForestG Mar 21 '18 at 11:53
  • Yes man I'm using angular 5 and i'm stuck for hours due to this. – Asad Shah Mar 21 '18 at 11:55
  • this is really strange. Could you please extend your question with the full implementation of the RouteGuard service? – ForestG Mar 21 '18 at 12:05
  • 1
    Hey @ForestG thanks there was an issue in my canActivate method. I injected ActivatedRouteSnapshot provider in service constructor instead of canActivate. Cheers dude :) – Asad Shah Mar 21 '18 at 12:13