簡體   English   中英

在CanActivate guard中獲取目標路由URL Lazy加載模塊Angular

[英]Get target route URL in CanActivate guard Lazy load modules Angular

我正在嘗試在canActivate后衛中獲取目標網址,但無法這樣做。 我已經配置preloadingStrategy: PreloadAllModulesRouterModule.forRoot當我使用ActivatedRoute其url屬性不包含路徑。 這是我的兩個模塊:

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 { }

我需要在RouteGuard中激活路由。

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

這個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM