Javascript 如何在登录表单提交时使用switchMap?
如何在提交登录表单时使用switchMap操作符,使表单不会多次提交到后端 我试过使用主题,但它对我不起作用。 这是我的密码Javascript 如何在登录表单提交时使用switchMap?,javascript,angular,typescript,rxjs,Javascript,Angular,Typescript,Rxjs,如何在提交登录表单时使用switchMap操作符,使表单不会多次提交到后端 我试过使用主题,但它对我不起作用。 这是我的密码 import { Subject } from 'rxjs'; import { Component, Output } from '@angular/core'; private submitStream = new Subject<Event>(); @Output() observ = this.submitStream.asObservable();
import { Subject } from 'rxjs';
import { Component, Output } from '@angular/core';
private submitStream = new Subject<Event>();
@Output() observ = this.submitStream.asObservable();
formSubmit(event: Event) {
this.submitStream.next(event);
}
如何让它工作?
还是不使用主题的简单方式
预期的行为是,每当用户多次单击submit按钮时,http submit post将被取消,只有最后一次单击将转到后端。好的,我已经找到了答案,只是在构造函数中使用了这个选项,它工作得很好:
this.submitStream.pipe(switchMap(() => this.authService.login(this.f.username.value, this.f.password.value)))
.subscribe((result) => {
this.router.navigate(['dashboard']);
}, (err) => {
this.error = err;
this.modalService.showSnackError(err.error.message);
});
我想你在用核导弹打苍蝇。为什么不干脆禁用按钮,直到你得到服务的响应?@Brandon我现在就是这样处理的,但出于好奇,我想知道。
this.submitStream.pipe(switchMap(() => this.authService.login(this.f.username.value, this.f.password.value)))
.subscribe((result) => {
this.router.navigate(['dashboard']);
}, (err) => {
this.error = err;
this.modalService.showSnackError(err.error.message);
});