gpt4 book ai didi

多次调用Angular 4 Subscribe方法

转载 作者:太空狗 更新时间:2023-10-29 18:08:16 26 4
gpt4 key购买 nike

我正在创建一个全局模态组件。我的问题是,当我调用 subscribe 方法时,它会根据调用的模态数多次调用。如何防止对可观察的订阅方法进行多次调用?请在下面检查我的代码。提前致谢。

modal.model.ts

export class Modal {
title: string;
message: string;
visible: boolean = false;
}

模态服务

import { Injectable } from '@angular/core';
import { Modal } from './modal.model';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class ModalService {
static readonly YES = 1;
static readonly NO = 0;

private modal = new Modal();
private subject = new Subject<Modal>();
private action = new Subject<number>();

confirmationDialog(message) {
this.modal.title = 'Confirmation';
this.modal.message = message;
return this;
}

show() {
this.modal.visible = true;
this.setModal(this.modal);
return this;
}

setAction(action: number) {
this.action.next(<number>action);
}

getAction(): Observable<any> {
return this.action.asObservable();
}

setModal(alert: Modal) {
this.subject.next(<Modal>alert);
return this;
}

getModal(): Observable<any> {
return this.subject.asObservable();
}
}

modal.component

import { Component, OnInit } from '@angular/core';
import { ModalService } from './modal.service';
import { Modal } from './modal.model';

@Component({
selector: 'modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
public modal: Modal;

constructor(private modalService: ModalService){}

ngOnInit() {
this.modalService.getModal().subscribe((modal: Modal) => {
this.modal = modal;
console.log(modal);
});
}

no() {
this.modalService.setAction(0);
this.modalService.close();
}

yes() {
this.modalService.setAction(1);
this.modalService.close();
}
}

调用模态组件

showModal() {
this.modalService.confirmationDialog('Are you sure you want to save this record?').show();
this.modalService.getAction().subscribe(response => {
if(response === ModalService.NO) {
return;
}
console.log('call mutiple times');
});
}

这是控制台日志输出的屏幕截图。 console log output

最佳答案

我认为在模态组件中使用 async 管道会更容易:

import { Component } from '@angular/core';
import { Observable } from 'rxjs';

import { ModalService } from './modal.service';
import { Modal } from './modal.model';

@Component({
selector: 'modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent {
public modal$: Observable<Modal>;

constructor(private modalService: ModalService){
this.modal$ = this.modalService.getModal();
}

no() {
this.modalService.setAction(0);
this.modalService.close();
}

yes() {
this.modalService.setAction(1);
this.modalService.close();
}
}

例如在你的模板中:

(modal$ | async)?.title

这确保 Angular 在组件销毁时自行清理订阅。

对于创建模式时的订阅,您可以使用 take 运算符,如果发出 x 值,它会完成订阅。

this.modalService.getAction().take(1).subscribe(response => {
if(response === ModalService.NO) {
return;
}
console.log('call mutiple times');
});

我假设您只需要 1 个值(因为它是一个简单的对话框)。

关于多次调用Angular 4 Subscribe方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327850/

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