小编mhl*_*hld的帖子

Angular Inject() 函数不提供到 CanActivateFn 的路由器

我正在尝试使用 Angular v14 功能之一,即 CanActivateFn。

不幸的是,当我的守卫被执行并且返回的语句为假时,我没有被重定向。正在抛出错误:

未捕获(承诺中): TypeError:router.parseUrl 不是函数 TypeError:router.parseUrl 不是函数

允许实体.guard.fn.ts

export function AllowedEntitiesGuard(allowedEntities: string[]): CanActivateFn {
  const router = Inject(Router);
  return (route: ActivatedRouteSnapshot) => {
    const entitiesTypeParameter = route.paramMap.get('entitiesType')

    return allowedEntities.indexOf(entitiesTypeParameter) !== -1 ? true : router.parseUrl('/');
  };
}
Run Code Online (Sandbox Code Playgroud)

主要.ts

bootstrapApplication(AppComponent, {
  providers: [
    provideHttpClient(withInterceptorsFromDi()),
    {
      provide: HTTP_INTERCEPTORS,
      useClass: JwtInterceptor,
      multi: true,
    },
    { provide: APP_CONFIG, useValue: environment },
    provideRouter(APP_ROUTES),
  ],
});
Run Code Online (Sandbox Code Playgroud)

当我尝试 console.log 路由器时,它向我显示其函数 ParamDecorator 实例。我做错了什么?

angular-routing angular angular-router

4
推荐指数
1
解决办法
6089
查看次数

标签 统计

angular ×1

angular-router ×1

angular-routing ×1