gpt4 book ai didi

angular - 关闭对话框组件生效

转载 作者:行者123 更新时间:2023-12-04 01:37:49 25 4
gpt4 key购买 nike

我需要从@ngrx/effect 关闭 Angular Material 对话框

这是我的代码

import { MatDialogRef, MatDialog } from "@angular/material/dialog";
import { AddComponent } from "./../../add/add.component";

@Injectable()
export class UserEffects {
@Effect()
addNewUser$ = this.actions$.pipe(
ofType(actions.UserActionTypes.addNewUser),
mergeMap((user: actions.addNewUser) =>
this.userService.createUser(user.user).pipe(
map(() => {
new actions.LoadUsers(),
this.notificationService.success("User added successfully!");
this.dialogRef.close(); <------ // here i try to close
}),
catchError(error => of(new actions.Error(error.error)))
)
)
);

constructor(
private actions$: Actions<actions.UserActions>,
private userService: UserService,
private notificationService: NotificationPopUpServiceService,
public dialogRef: MatDialogRef<AddComponent>
) {}
}

有了这个我得到错误

main.ts:13 NullInjectorError: R3InjectorError[EffectsRootModule -> InjectionToken ngrx/effects: Root Effects -> UserEffects -> MatDialogRef -> MatDialogRef -> MatDialogRef]: NullInjectorError: No provider for MatDialogRef!



从效果或服务中关闭 Material 对话框的最佳方法是什么?因为我们总是需要为对话框组件设置一个名称?

谢谢

最佳答案

我认为关闭对话框的最佳解决方案是订阅 effect 变量
IE

// close the dialog
// Inside the dialog component
this.userEffects.addNewUser$.pipe(
ofType(Actions.LoadUsers)
).subscribe(_ => this.matDialogRef.close());

关于angular - 关闭对话框组件生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58850472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com