2

我正在尝试将新的自定义 DataService 添加到 @ngrx/data

我正在通过 custom-entity-data-service.ts 扩展 DefaultDataService 类,然后将其注册到 entityService 以将服务作为 DI

自定义实体数据service.ts

第 1 步 - 定义服务

import { Injectable } from '@angular/core';
import { DefaultDataService, HttpUrlGenerator, DefaultDataServiceConfig } from '@ngrx/data';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SharepointService } from 'dnp-sharepoint';

// @Injectable() << NOTE - this commented out
export class SharePointEntityDataService extends DefaultDataService<any>{

  constructor( entityName: string,  http: HttpClient, 
     httpUrlGenerator: HttpUrlGenerator, config: DefaultDataServiceConfig, private sharePoint: SharepointService) {
    super(entityName, http, httpUrlGenerator, config);
}
  
getAll(): Observable<any[]>{
  console.log('calling for ',this.entityName);
  return this.sharePoint.getAll(this.entityName); // << my custom call 
}

}

app-store.module.ts << 封装所有ngrx/data东西的模块

第 2 步 - 在模块类中为实体注册服务

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    StoreModule.forRoot({}),
    EffectsModule.forRoot([]),
    EntityDataModule.forRoot(entityConfig),
    environment.production ? [] : StoreDevtoolsModule.instrument(),
  ],
  providers: [Store, SharePointEntityDataService]
})
export class AppStoreModule {
  constructor(private entityDataService: EntityDataService, private sharepointEntityDataService: SharePointEntityDataService) {
    this.entityDataService.registerService('Registry', this.sharepointEntityDataService);
  }
}

simple.component.ts

第 3 步 - 在组件中使用它

  constructor(private registryService: RegistryService, private entityDataService: EntityDataService, 
    private sharePointDataService: SharePointEntityDataService) { 
      this.registryService.getAll().subscribe(obj=>console.log('ger all ...',obj));
    }
ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[SharePointEntityDataService -> String]: 
  StaticInjectorError(Platform: core)[SharePointEntityDataService -> String]: 
    NullInjectorError: No provider for String!
NullInjectorError: StaticInjectorError(AppModule)[SharePointEntityDataService -> String]: 
  StaticInjectorError(Platform: core)[SharePointEntityDataService -> String]: 

我认为错误是由于一些依赖注入,但无法弄清楚。任何建议将不胜感激。

谢谢

4

0 回答 0