angular - 当过滤器更改或使用 NGRX 创建新实体时更新 redux 状态
问题描述
我正在使用 Angular 9 和NGRX创建一个应用程序,其中我有一个帖子类:
class Post {
id: number;
title: string;
category: string;
}
ParentComponent
显示第 1 页的帖子及其子项,ChildComponent
显示第 1 页的帖子和category=book
.
在任何时候,页面或类别可能会发生变化,或者可能会创建新帖子......
数据库很大,所以我无法将所有帖子加载到 Angular 应用程序中。
我有一个PostService
GetByQuery 方法:
class PostService {
GetByQuery(category: string, page: number) : Observable<Post[]> {
// implementation
}
}
page
如果orcategory
更改或新post
创建的,如何更新状态?
解决方案
不幸的是,提供的信息是不够的,因为它没有显示你有哪些动作和减速器。
通常对于这样的事情,当我们需要做一些除了减少人们effects
与他们一起使用的动作之外的事情时,您可以监听 的变化page
,category
或者新的post
并导致另一个动作,例如reload
数据。
在这里您可以找到所有信息:https ://ngrx.io/guide/effects
一个示例(它是带有装饰器的旧示例,目前应该使用 createEffect 函数)。
@Injectable()
export class ActivationAdminEffects {
/**
* Sending activation by admin request and processing its result.
*/
@Effect()
public readonly activate$: Observable<Action> = this.actions$.pipe(
ofType(ActivationAdminActions.Activate), // <- waiting this action
// doing side things we need
mergeMap((action: ActivationAdminActionsActivate) =>
this.http.post(this.urlActivationAdmin, action.data).pipe(
mergeMap(() => {
// dispatching a new action once we are done.
return of(new ActivationAdminActionsActivateSuccess());
}),
catchError(
(error: HttpErrorResponse): Observable<Action> => {
if (error.status === 404) {
// an expected error and dispatch of related action
return of(new ActivationAdminActionsActivateFailure());
}
return throwError(error);
},
),
),
),
);
protected readonly urlActivationAdmin: string = `${environment.urlApi}user/activation-admin`;
constructor(protected readonly actions$: Actions, protected readonly http: HttpClient) {}
}
推荐阅读
- modelica - 在 Modelica 模拟中包含因果关系会导致模型展平时出现平移错误
- angular - Angular 2+ 单元测试-fixture.detectChanges() 删除组件属性
- ios - 错误域 = NSCocoaErrorDomain 代码 = 3840 “字符 1 周围的值无效。” UserInfo={NSDebugDescription=字符 1 周围的值无效。swift 4
- python - 用于匹配特定后缀域的 url 正则表达式
- azure - 如何在 Azure SQL 数据库上配置邮件
- java - Jhipster:应该为用户文件设置什么路径
- sharepoint - 共享策略
- mysql - mysql:查询组合来自 2 个表的信息
- reactjs - TypeScript:如何在 React 的 props 中传递组件时定义类型?
- c++ - 如何编译包括 Google Cloud Storage 库的 C++ 代码?