StaticInjectorError(AppModule)[NbIsGrantedDirective -> NbAccessChecker]

Posted

技术标签:

【中文标题】StaticInjectorError(AppModule)[NbIsGrantedDirective -> NbAccessChecker]【英文标题】: 【发布时间】:2019-06-10 23:50:28 【问题描述】:

我正在使用 NbAuthJWTToken 和 ngx-admin 进行身份验证,但出现此错误:

错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)[NbIsGrantedDirective -> NbAccessChecker]: StaticInjectorError(平台:核心)[NbIsGrantedDirective -> NbAccessChecker]: NullInjectorError:没有 NbAccessChecker 的提供者! 错误:: StaticInjectorError(平台:核心)[NbIsGrantedDirective -> NbAccessChecker]: NullInjectorError:没有 NbAccessChecker 的提供者! 在 NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:717) 在 resolveToken (core.js:954) 在 tryResolveToken (core.js:898) 在 StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:795) 在 resolveToken (core.js:954) 在 tryResolveToken (core.js:898) 在 StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:795) 在 resolveNgModuleDep (core.js:17739) 在 NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:18428) 在 resolveNgModuleDep (core.js:17739) 在 resolvePromise (zone.js:814) 在 resolvePromise (zone.js:771) 在 zone.js:873 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) 在 Object.onInvokeTask (core.js:14134) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420) 在 Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188) 在 drainMicroTaskQueue (zone.js:595)

这是我的文件内容: app.module.ts:

/**
 * @license
 * Copyright Akveo. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
import  APP_BASE_HREF  from '@angular/common';
import  BrowserModule  from '@angular/platform-browser';
import  BrowserAnimationsModule  from '@angular/platform-browser/animations';
import  NgModule  from '@angular/core';
import  HttpClientModule  from '@angular/common/http';
import  CoreModule  from './@core/core.module';

import  AppComponent  from './app.component';
import  AppRoutingModule  from './app-routing.module';
import  ThemeModule  from './@theme/theme.module';
import  NgbModule  from '@ng-bootstrap/ng-bootstrap';
import  AppService  from './app.service';

@NgModule(
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    AppRoutingModule,

    NgbModule.forRoot(),
    ThemeModule.forRoot(),
    CoreModule.forRoot(),
  ],
  bootstrap: [AppComponent],
  providers: [
     provide: APP_BASE_HREF, useValue: '/' ,
    AppService
  ],
)
export class AppModule 

app.routing:

import  ExtraOptions, RouterModule, Routes  from '@angular/router';
import  NgModule  from '@angular/core';
import  AuthGuard  from './guards/auth.guard';

const routes: Routes = [
   path: 'pages',canActivate: [AuthGuard], loadChildren: './pages/pages.module#PagesModule' ,
  
    path: 'auth',
    loadChildren: './@theme/components/auth/auth.module#AuthModule',
  ,
   path: '', redirectTo: 'pages', pathMatch: 'full' ,
   path: '**', redirectTo: 'pages' ,
];

const config: ExtraOptions = 
  useHash: true,
;

@NgModule(
  imports: [RouterModule.forRoot(routes, config)],
  exports: [RouterModule],
)
export class AppRoutingModule 

核心模块:

import 
  ModuleWithProviders,
  NgModule,
  Optional,
  SkipSelf
 from "@angular/core";
import  CommonModule  from "@angular/common";
import 
  NbAuthModule,
  NbDummyAuthStrategy,
  NbPasswordAuthStrategy,
  NbAuthJWTToken
 from "@nebular/auth";
import  NbSecurityModule, NbRoleProvider  from "@nebular/security";
import  of as observableOf  from "rxjs";

import  throwIfAlreadyLoaded  from "./module-import-guard";
import  DataModule  from "./data/data.module";
import  AnalyticsService  from "./utils/analytics.service";
import  environment  from './../../environments/environment';

const socialLinks = [
  
    url: "https://github.com/akveo/nebular",
    target: "_blank",
    icon: "socicon-github"
  ,
  
    url: "https://www.facebook.com/akveo/",
    target: "_blank",
    icon: "socicon-facebook"
  ,
  
    url: "https://twitter.com/akveo_inc",
    target: "_blank",
    icon: "socicon-twitter"
  
];

export const NB_CORE_PROVIDERS = [
  ...DataModule.forRoot().providers,
  ...NbAuthModule.forRoot(
    strategies: [
      NbPasswordAuthStrategy.setup(
        name: "email",
        token: 
          class: NbAuthJWTToken,
          key: "token"
        ,
        baseEndpoint: environment.apiEndpoint,
        login: 
          endpoint: "/login",
          method: "post"
        ,
        requestPass: 
          endpoint: "/forgot",
          method: "post",
          redirect: 
            success: '/auth/reset-password',
            failure: null,
          ,
        ,
        resetPass: 
          endpoint: "/reset",
          method: "put",
          redirect: 
            success: '/auth/login',
            failure: null,
          ,
          resetPasswordTokenKey: 'reset_password_token',
        ,
        logout: 
          alwaysFail: false,
          endpoint: '/logout',
          method: 'delete',
          redirect: 
            success: '/auth/login',
            failure: null,
          ,
        ,
        register: 
          alwaysFail: false,
          endpoint: '/register',
          method: 'post',
          redirect: 
            success: '/auth/login',
            failure: null,
          ,
        
      )
    ],
    forms: 
      login: 
        redirectDelay: 0,
        strategy: 'email',
        rememberMe: true,
        showMessages: 
          success: true,
        ,
      ,
      register: 
        redirectDelay: 0,
        showMessages: 
          success: true,
        ,
      ,
      requestPassword: 
        redirectDelay: 0,
        showMessages: 
          success: true,
        ,
      ,
      resetPassword: 
        redirectDelay: 0,
        showMessages: 
          success: true,
        ,
      ,
      logout: 
        redirectDelay: 500,
      ,
      validation: 
        name: 
          required: true,
        ,
        phone: 
          required: true,
          minLength: 8,
          maxLength: 12,
        ,
        password: 
          required: true,
        ,
        email: 
          required: true
        ,
        idNumber: 
          required: true,
          minLength: 8,
          maxLength: 12,
        ,
      ,
    
  ).providers,
  AnalyticsService
];

@NgModule(
  imports: [CommonModule],
  exports: [NbAuthModule],
  declarations: []
)
export class CoreModule 
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) 
    throwIfAlreadyLoaded(parentModule, "CoreModule");
  

  static forRoot(): ModuleWithProviders 
    return <ModuleWithProviders>
      ngModule: CoreModule,
      providers: [...NB_CORE_PROVIDERS]
    ;
  

我还将 nebular auth 组件添加到我的项目和保护文件夹中

【问题讨论】:

【参考方案1】:

你应该在你的 AppModule 中导入 NbSecurityModule。它为我解决。

【讨论】:

【参考方案2】:

在你的 app.module.ts 文件中添加:

进口:[ ... NbSecurityModule.forRoot(), ... ]

【讨论】:

嗨,感谢您的回答,我在处理 NbSidebarModule 时解决了类似的错误【参考方案3】:

您发布的错误说您正在使用NbIsGrantedDirective,并且此指令需要NbAccessChecker。要提供NbAccessChecker,请安装npm i @nebular/security 并将SecurityModule 导入您的应用程序。您可以在here 找到详细文档。

【讨论】:

我已经安装并导入 NbSecurity 模块到主题模块中。我也关注这个文档,但同样的错误【参考方案4】:

你需要提供这个也可以查看 ngx-admin 以获得 core.module.ts 中的完整示例 提供:NbRoleProvider,使用类:NbSimpleRoleProvider,

【讨论】:

以上是关于StaticInjectorError(AppModule)[NbIsGrantedDirective -> NbAccessChecker]的主要内容,如果未能解决你的问题,请参考以下文章

ERROR 错误:StaticInjectorError(AppModule)[StorieControllerComponent -> DataService]:

如何修复“错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]”

NullInjectorError:在Angular 2中测试时的StaticInjectorError(DynamicTestModule)

为啥我会在角度 6 中得到 StaticInjectorError。这意味着啥

如何修复错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[HomeComponent - > HttpHeaders]

core.js:4002 ERROR 错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[Table -> FilterSer