当行为主题值发射时,如何触发http调用switchmap中的http调用
我正在学习用声明性编写角度应用程序,但我不确定在调用帖子请求时最好的方法是什么。
我有一个带有电子邮件和密码表单的登录表单,当用户输入和单击时,提交将
onLogin() {
if (this.loginForm?.valid) {
this.userService.loginUserSubject.next(this.loginForm?.value)
}
}
在我的用户内拨打主题.next()。服务类我遵循的服务类:
loginUserSubject = new BehaviorSubject<IUser | null>(null);
loginInfo$ = this.loginUserSubject.asObservable();
$login = this.loginInfo$.pipe(
switchMap(user => this.http.put<IUser>('/user/login', user).pipe(
tap(response => localStorage.setItem('token', response.email)),
catchError(error => throwError(error))
)
)
)
bu,bu bu,则用户单击“未能发生”,该表格,表格。值将散发到主题,但不会执行HTTP调用,因为登录$可观察的订阅没有发生。当受试者获取值而无需订阅我的组件中的可观察到的值或使用异步管道时,我是否可以调用HTTP POST请求?
I'm learning to write angular applications in a declarative and I'm not sure what the best approach is when calling POST requests.
I have a login form with email and password forms which when a user enters and clicks submit will call the subject.next()
onLogin() {
if (this.loginForm?.valid) {
this.userService.loginUserSubject.next(this.loginForm?.value)
}
}
Inside my user.service class I have following implemented:
loginUserSubject = new BehaviorSubject<IUser | null>(null);
loginInfo$ = this.loginUserSubject.asObservable();
$login = this.loginInfo$.pipe(
switchMap(user => this.http.put<IUser>('/user/login', user).pipe(
tap(response => localStorage.setItem('token', response.email)),
catchError(error => throwError(error))
)
)
)
Bu then the user clicks submit nothing will happen, the form.value will emit to the subject but the HTTP call won't be executed because there is no subscription happening for the login$ Observable. Is there a way I can call the HTTP POST request when the subject gets the value without subscribing to the Observable in my component or using the async pipe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么您需要在此处进行
cistionubject
。 IMO,您可以没有它做。至于为什么目前不适合您,您很可能还没有订阅
$ login
可观察到的。服务
组件
I'm not sure why you'd need the
BehaviorSubject
here. IMO, you could make do without it.As for why it isn't working for you at the moment, most probably you haven't subscribed to the
$login
observable.Service
Component
仅仅因为您没有订阅
主题
。代码应该像。Just because you doesn't subscribe your
Subject
. The code should be like.