NgRx 数据 DefaultDataServiceConfig

Ser*_*nar 3 ngrx angular angular-ngrx-data

我刚刚开始在我的项目中使用NgRx Data,我搜索了几个小时,但找不到我面临的问题的解决方案。

我正在尝试使用DefaultDataServiceConfig来更改调用的默认根,如下所示:

应用程序模块.ts

export const defaultDataServiceConfig: DefaultDataServiceConfig = {
  root: `${environment.apiUrl}/`,
};

@NgModel({
   ...
   providers: [
      {
         provide: DefaultDataServiceConfig
         useValue: defaultDataServiceConfig
      }
   ]
});
Run Code Online (Sandbox Code Playgroud)

环境.ts

export const environment = {
   ...
   apiUrl: 'https://my-api.com/v1',
};
Run Code Online (Sandbox Code Playgroud)

但是,调用将转到localhost http://localhost:3000/client/list而不是my-api.com https://my-api.com/v1/client/list

我缺少什么吗?我找不到任何示例,而且ngrx.io文档也很糟糕。

小智 8

我遇到了同样的问题。

检查DefaultDataService Doc,其构造函数中的第四个参数是可选的,默认情况下未定义。当您扩展此类创建自己的服务时,请传递配置参数,如下所示:

@Injectable()
export class EmployersDataService extends DefaultDataService<Employer> {
  constructor(
    http: HttpClient,
    httpUrlGenerator: HttpUrlGenerator,
    config: DefaultDataServiceConfig
  ) {
    super('Employer', http, httpUrlGenerator, config);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,app.module.ts 中提供的 DefaultDataServiceConfig 将起作用。