我正在实现 ngrx 数据,但我没有在存储中获取数据。但是,我可以使用 POSTMAN 通过我的 .net core web api 查看数据。
在控制台中查看商店时,我观察到一些未定义的值。
在 ngrx devtools 我看到以下事件
获取以下 API 响应:
[
{
"name": "Casados",
"id": 1,
},
{
"name": "Divorciados",
"id": 2,
}
]
功能模块如下:
const entityMetadata: EntityMetadataMap = {
CivilStatus: {
sortComparer: sortByName,
entityDispatcherOptions: {
optimisticUpdate: false,
optimisticDelete: false,
},
},
};
function sortByName(a: CivilStatus, b: CivilStatus): number {
let comp = a.Name.localeCompare(b.Name);
return comp;
}
@NgModule({
declarations: [CivilStatusComponent, UpdateCivilStatusComponent, ListCivilStatusComponent, AddCivilStatusComponent],
imports: [
CommonModule,
CivilStatusRoutingModule,
SharedModule,
HttpClientModule,
ToastrModule.forRoot(),
],
providers: [
CivilStatusResolver,
CivilStatusService,
OverlayService,
CivilStatusDataService
// NotificationService
]
})
export class CivilStatusModule {
constructor(
private entityDefinitionService: EntityDefinitionService,
private entityDataService: EntityDataService,
private civilStatusDataService: CivilStatusDataService
)
{
entityDefinitionService.registerMetadataMap(entityMetadata);
entityDataService.registerServices({
'CivilStatus': civilStatusDataService
});
}
}
服务实现如下:
export class CivilStatusDataService extends DefaultDataService<CivilStatus> {
baseURL: string = environment.cuatrocienegasapi;
apiURL: string = 'api/CivilStatus/';
constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
super('CivilStatus', http, httpUrlGenerator);
}
public getAll(): Observable<CivilStatus[]> {
try {
return this.http.get<CivilStatus[]>(this.baseURL + this.apiURL)
.pipe(map(civilstatuses => civilstatuses.map (civilstatus => this.mapEntity(civilstatus))));
} catch (error) {
error.message =
'Error in fetching all civilstatus methodName:get() ';
throw error;
}
}
public getById(id:number): Observable<CivilStatus> {
try {
return this.http.get<CivilStatus>(this.baseURL + this.apiURL + id);
} catch (error) {
error.message =
'Error in civilstatus to fetch civilstatus methodName:getById(Id:number) ';
throw error;
}
}
add(civilstatus: CivilStatus): Observable<CivilStatus> {
try {
return this.http
.post<{result: number}>(
this.baseURL + this.apiURL,
civilstatus
)
.pipe(
map((data) => {
return { ...civilstatus };
})
);
} catch (error) {
error.message =
'Error in inseting record methodName:saveData() ';
throw error;
}
}
update(civilStatus: Update<CivilStatus>): Observable<CivilStatus> {
try{
return this.http.put<CivilStatus>(
this.baseURL + this.apiURL,
{ ...civilStatus.changes }
);
} catch (error) {
error.message =
'Error in updating record methodName:updateData() ';
throw error;
}
}
delete(id: number): Observable<number> {
try {
return this.http
.delete<number>(this.baseURL + this.apiURL + id)
.pipe(
map((data) => {
return id;
})
);
} catch (error) {
error.message =
'Error in updating record methodName:updateData() ';
throw error;
}
}
private mapEntity(civilStatus: CivilStatus): CivilStatus {
return {
...civilStatus
}
}
}
在 App.module.ts 中,我添加了以下内容:
StoreModule.forRoot([]),
EffectsModule.forRoot([]),
EntityDataModule.forRoot({})