I just started to use NgRx Data on my project and I'm searching for a couple of hours and I can't find a solution for a problem that I'm facing with.
I'm trying to change the default root of calls by using DefaultDataServiceConfig like that:
app.module.ts
export const defaultDataServiceConfig: DefaultDataServiceConfig = {
root: `${environment.apiUrl}/`,
};
@NgModel({
...
providers: [
{
provide: DefaultDataServiceConfig
useValue: defaultDataServiceConfig
}
]
});
environment.ts
export const environment = {
...
apiUrl: 'https://my-api.com/v1',
};
However, the call is going to localhost http://localhost:3000/client/list instead of going to my-api.com https://my-api.com/v1/client/list.
Is there something that I'm missing? I can't find any examples and ngrx.io documentation is a poor one.
I faced the same problem.
Check the DefaultDataService Doc, the 4th argument in its constructor is optional and undefined by default. When you extend this class creating your own service pass the config argument like this:
@Injectable()
export class EmployersDataService extends DefaultDataService<Employer> {
constructor(
http: HttpClient,
httpUrlGenerator: HttpUrlGenerator,
config: DefaultDataServiceConfig
) {
super('Employer', http, httpUrlGenerator, config);
}
}
Then, the DefaultDataServiceConfig provided in app.module.ts will work.
You should import it on the same level where you import EntityDataModule
@NgModule({
declarations: [
DataComponent,
],
imports: [
CommonModule,
EntityDataModule.forRoot(entityConfig)
],
providers: [
{
provide: DefaultDataServiceConfig,
useValue: {
root: environment.apiUrl,
},
}
],
exports: [
DataComponent,
],
})
If for whatever reason it doesn't work for you - you can provide your own HttpUrlGenerator, it resolves urls for every entity.
export abstract class HttpUrlGenerator {
abstract entityResource(entityName: string, root: string): string;
abstract collectionResource(entityName: string, root: string): string;
abstract registerHttpResourceUrls(
entityHttpResourceUrls?: EntityHttpResourceUrls
): void;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With