【问题标题】:Angular Upgrade for Performance No provider for $rootscope, $locationAngular 性能升级没有 $rootscope、$location 的提供者
【发布时间】:2020-11-26 15:18:18
【问题描述】:

我已经使用 Angular 升级方法设置了 Angular Hybrid 设置,并且能够毫无问题地运行应用程序。

但是,我在运行 Angular 端测试用例时遇到了问题, 我已经提供了组件、规格和所有相关的 sn-ps。谁能帮我解决可能是什么问题以及如何解决它。

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

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$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)

【问题讨论】:

    标签: angularjs angular angular-test angular-hybrid


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2018-04-26
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2018-11-17
      • 2018-08-04
      相关资源
      最近更新 更多