当组件初始化时,Angular 14行为主题是返回Ngoninit()中的先前值

发布于 2025-02-10 21:03:32 字数 1651 浏览 2 评论 0原文

我有一个以下ngoninit和保存方法的组件。当执行保存操作时,我有一个行为主体,我正在使用该行为。默认值为null。但是,在我保存并重新打开组件之后,它将提出以前的值,并再次拨打服务呼叫。我在这里做错了什么?

组件:

ngOnInit(): void {
    this.templateSaveService.templateSaveSubject
        .pipe(
            takeUntil(this.destroy$),
            filter((auditTemplate) => auditTemplate !== null),
            switchMap((auditTemplate) => {
                return this.auditTemplateSaveService 
                    .createTemplate(auditTemplate) //it is getting called for first time as well.
                    .pipe(
                        map((response) => ({
                            ...auditTemplate,
                            id: response.body.id
                        })),
                        catchError((error) => this.handleError(error))
                    );
            })
        )
        .subscribe((template) => {
            this.isSavingData = false;
            this.toasterService.success('Template has been saved successfully');
        });     
}

save(): void {
    this.templateSaveService.saveAuditTemplate(this.auditTemplate);
}

服务:

templateSaveSubject = new BehaviorSubject<AuditTemplate>(null);

saveAuditTemplate(auditTemplate: AuditTemplate): void {
    this.templateSaveSubject.next(updatedTemplate);

clearSaveSubject(): void {
    this.templateSaveSubject .next(null);
}

我可以通过将主题设置为组件ngdestroy()中的null来解决,

ngOnDestroy(): void {
    this.auditTemplateSaveService.clearSaveSubject();
    this.destroy$.next(true);
    this.destroy$.complete();
}

但想知道为什么最初未清除行为主题值,并且对此有其他更好的解决方案吗?

I have a component with following ngOnInit and save method. I have a behaviorsubject which i am using tonotify when save action is performed. Default value is null. but after i save and reopen the component, it is coming up with previous value and again making service call which it shouldn't. What am i doing wrong here?

component:

ngOnInit(): void {
    this.templateSaveService.templateSaveSubject
        .pipe(
            takeUntil(this.destroy$),
            filter((auditTemplate) => auditTemplate !== null),
            switchMap((auditTemplate) => {
                return this.auditTemplateSaveService 
                    .createTemplate(auditTemplate) //it is getting called for first time as well.
                    .pipe(
                        map((response) => ({
                            ...auditTemplate,
                            id: response.body.id
                        })),
                        catchError((error) => this.handleError(error))
                    );
            })
        )
        .subscribe((template) => {
            this.isSavingData = false;
            this.toasterService.success('Template has been saved successfully');
        });     
}

save(): void {
    this.templateSaveService.saveAuditTemplate(this.auditTemplate);
}

service:

templateSaveSubject = new BehaviorSubject<AuditTemplate>(null);

saveAuditTemplate(auditTemplate: AuditTemplate): void {
    this.templateSaveSubject.next(updatedTemplate);

clearSaveSubject(): void {
    this.templateSaveSubject .next(null);
}

I can fix by set the subject to null in component ngDestroy()

ngOnDestroy(): void {
    this.auditTemplateSaveService.clearSaveSubject();
    this.destroy$.next(true);
    this.destroy$.complete();
}

But would like to know why the behaviour subject value not cleared initially and is there any other better solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2025-02-17 21:03:32

我看不到您的整个代码,但我想您的服务是在根目录中提供的,这意味着一旦您的组件被破坏,它就不会被破坏。

如果我的假设正确,您有2个选项 -

  1. 您可以在组件级别而不是应用程序root提供服务。
  2. 您可以维护组件中的行为主题。

I don't see your whole code but I guess that your service is provided in the root, which means that it won't get destroyed once your component gets destroyed.

if my assumptions are correct you have 2 options -

  1. u can provide the service at the component level instead of the app root.
  2. you can maintain the behavior subject in your component.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文