英文:
How convert NGRX Effect to NGRX Effect v15
问题
我正在尝试将以下代码转换为 NGRX 15:
我是Angular新手,需要帮助。
"@ngrx/effects": "^15.4.0"
@Injectable()
export class SnackbarEffects {
@Effect({
dispatch: false
})
closeSnackbar: Observable<any> = this.actions.ofType(SNACKBAR_CLOSE)
.pipe(
tap(() => this.matSnackBar.dismiss())
);
@Effect()
showSnackbar: Observable<any> = this.actions.ofType<SnackbarOpen>(SNACKBAR_OPEN)
.pipe(
map((action: SnackbarOpen) => action.payload),
tap(payload => this.matSnackBar.open(payload.message, payload.action, payload.config)),
delay(2000),
map(() => new SnackbarClose())
);
constructor(private actions: Actions,
private matSnackBar: MatSnackBar) {
}
}
英文:
I am trying to convert the following code in NGRX 15
I'm new in angular and need to help.
"@ngrx/effects": "^15.4.0"
@Injectable()
export class SnackbarEffects {
@Effect({
dispatch: false
})
closeSnackbar: Observable<any> = this.actions.ofType(SNACKBAR_CLOSE)
.pipe(
tap(() => this.matSnackBar.dismiss())
);
@Effect()
showSnackbar: Observable<any> = this.actions.ofType<SnackbarOpen>(SNACKBAR_OPEN)
.pipe(
map((action: SnackbarOpen) => action.payload),
tap(payload => this.matSnackBar.open(payload.message, payload.action, payload.config)),
delay(2000),
map(() => new SnackbarClose())
);
constructor(private actions: Actions,
private matSnackBar: MatSnackBar) {
}
}
答案1
得分: 2
@Injectable()
export class SnackbarEffects {
closeSnackbar: Observable<any> = createEffect(() => this.actions.pipe(
ofType(SNACKBAR_CLOSE),
tap(() => this.matSnackBar.dismiss())
),
{ dispatch: false}
);
// this should work by wrapping it with `createEffect`
showSnackbar: Observable<any> = createEffect(() => this.actions.pipe(
ofType<SnackbarOpen>(SNACKBAR_OPEN),
map((action: SnackbarOpen) => action.payload),
tap(payload => this.matSnackBar.open(payload.message, payload.action, payload.config)),
delay(2000),
map(() => new SnackbarClose())
));
constructor(private actions: Actions,
private matSnackBar: MatSnackBar) {
}
}
英文:
@Injectable()
export class SnackbarEffects {
closeSnackbar: Observable<any> = createEffect(() => this.actions .pipe(
ofType(SNACKBAR_CLOSE), // 👈 use ofType operator
tap(() => this.matSnackBar.dismiss())
),
{ dispatch: false} // 👈 move config as the 2nd arg
);
// this should work by wrapping it with `createEffect`
showSnackbar: Observable<any> = createEffect(() => this.actions
.pipe(
ofType<SnackbarOpen>(SNACKBAR_OPEN), // 👈 use ofType operator
map((action: SnackbarOpen) => action.payload),
tap(payload => this.matSnackBar.open(payload.message, payload.action, payload.config)),
delay(2000),
map(() => new SnackbarClose())
));
constructor(private actions: Actions,
private matSnackBar: MatSnackBar) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论