Angular性能升级没有提供$ rootscope,$ location的提供程序

时间:2020-08-06 11:05:38

标签: angularjs angular angular-testing angular-hybrid

我已经使用Angular升级设置了Angular Hybrid设置,以提高性能,并能够运行应用程序而没有任何问题。

但是,我在运行Angular侧面测试用例时遇到问题, 我提供了组件,规格和所有相关的代码片段。谁能帮助我解决可能出现的问题以及如何解决此问题。

如果需要更多详细信息,请告诉我

角度组件:componentA.ts

constructor(@Inject('$rootScope') public rootScope) { }

角度组件规格:componentA.spec.ts

TestBed.configureTestingModule({
  declarations: [ ComponentA ],
  providers: [
    {
      provide: '$rootScope',
      useFactory: ($injector: any) => $injector.get('$rootScope'),
      deps: ['$injector']
    }
  ],
  imports: [
    createAngularTestingModule([ 'app'])
  ]
})

错误

Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- $location
    at eval (webpack:///./node_modules/angular/angular.js?:68:12)
    at eval (webpack:///./node_modules/angular/angular.js?:4418:19)
    at Object.getService [as get] (webpack:///./node_modules/angular/angular.js?:4571:39)
    at eval (webpack:///./node_modules/angular/angular.js?:4423:45)
    at getService (webpack:///./node_modules/angular/angular.js?:4571:39)
    at injectionArgs (webpack:///./node_modules/angular/angular.js?:4595:58)
    at Object.invoke (webpack:///./node_modules/angular/angular.js?:4617:18)
    at eval (webpack:///./node_modules/angular/angular.js?:4424:37)
    at getService (webpack:///./node_modules/angular/angular.js?:4571:39)
    at injectionArgs (webpack:///./node_modules/angular/angular.js?:4595:58)
NullInjectorError: R3InjectorError(DynamicTestModule)[$rootScope -> $rootScope]: 
  NullInjectorError: No provider for $rootScope!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ '$rootScope', '$rootScope' ] })
    at NullInjector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:1085:1)
    at R3Injector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16984:1)
    at R3Injector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16984:1)
    at NgModuleRef$1.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:36608:1)
    at Object.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:34252:1)
    at getOrCreateInjectable (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5892:1)
    at ɵɵdirectiveInject (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:21251:1)

1 个答案:

答案 0 :(得分:2)

就像错误一样,表明您缺少@Injectable({ providedIn: 'root' }) 提供程序。您可以在$rootElement执行之前插入以下内容。

TestBed.configureTestingModule