Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在登录表单提交时使用switchMap?_Javascript_Angular_Typescript_Rxjs - Fatal编程技术网

Javascript 如何在登录表单提交时使用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();

如何在提交登录表单时使用switchMap操作符,使表单不会多次提交到后端

我试过使用主题,但它对我不起作用。 这是我的密码

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);
        });