我正在尝试通过创建简单的 TODO 应用程序来学习 @ngrx/data
我创建了简单的 ToDoDataService 来覆盖默认 HTTP
@Injectable()
export class TodosDataService extends DefaultDataService<Todo> {
constructor(httpClient: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
super('Todo', httpClient, httpUrlGenerator);
}
getAll(): Observable<Array<Todo>> {
console.log('asfsfsfsf')
return this.http.get('https://jsonplaceholder.typicode.com/todos')
.pipe(
map((res: any) => res)
);
}
getWithQuery(query): Observable<Array<any>> {
console.log('asfsfsfsf', query);
return this.http.get(`https://jsonplaceholder.typicode.com/todos?${query}`)
.pipe(
map((res: any) => res)
);
}
}
在路由解析器中,我正在这样做
@Injectable()
export class TodosResolver implements Resolve<boolean> {
constructor(private todoService: TodoEntityService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.todoService.loaded$
.pipe(
tap(loaded => {
if (!loaded) {
this.todoService.getAll();
}
}),
filter(loaded => !!loaded),
first()
);
}
}
这工作正常,商店/实体已更新
但在我的组件中,我正在这样做
export class HomeComponent implements OnInit {
loading$: Observable<boolean>;
todos$: Observable<Todo[]>;
constructor(
private todoService: TodoEntityService
) { }
ngOnInit() {
console.log('dsdf');
this.todos$ = this.todoService.entities$
.pipe(
tap(todos => {
console.log(todos);
this.loadMore();
console.log(todos);
}),
map((todos: any) => todos),
first()
);
}
loadMore() {
this.todoService.getWithQuery('_start=20&_limit=5');
}
}
这里 API 正在调用,但实体仍在显示旧数据。不知道我做错了什么,
请帮忙