简体   繁体   中英

Make http post with data from observable in Angular

I want to use data from observables that lives in my registerStore service to send them in the body of my http post request. How can I do so?

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import { RegisterStore } from './register.store';

@Injectable({ providedIn: 'root' })
export class RegisterService {
  constructor(
    private httpClient: HttpClient,
    private registerStore: RegisterStore
  ) {}

  signUp() {
    this.httpClient
      .post(
        `${environment.baseAuthUrl}/signup`,
        { email, password }, // <- this should come from registerStore.email$ and registerStore.password$
      )
  }
}

When you use signUp you trigger it, so you need to use the CURRENT STORE STATE VALUES of email and password, it is unclear what store solution you use but it can be something like: registerStore.state.email which returns the current value and NOT the observable of that value

signUp() {
    this.httpClient
      .post(
        `${environment.baseAuthUrl}/signup`,
        { registerStore.state.email, registerStore.state.password }, // <- this should come from registerStore.email$ and registerStore.password$
      )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM