Angular 性能升级 $rootscope, $location 没有供应商

Angular Upgrade for Performance No provider for $rootscope, $location

我已经设置了 Angular 混合设置,使用 Angular 性能方法升级并且能够 运行 应用程序没有任何问题。

但是,我面临一个问题 运行ning Angular 边测试用例, 我提供了组件、规格和所有相关片段。任何人都可以帮助我解决可能的问题以及如何解决它。

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

Angular 分量:componentA.ts

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

Angular 组件规格: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.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)

就像错误说您缺少 $rootElement 提供商。您可以通过在 TestBed.configureTestingModule 执行之前插入以下内容来实现。

angular.module(<your_angularjs_module>).provider({
   $rootElement: {
       $get: () => {
           return angular.element(document.getElementById(<your_root_angularjs_element_id>));
       }
   }
});