无法从其他组件Angular 8中的可观察数据中获取数据

时间:2019-10-08 17:41:16

标签: angular

我试图传递可疑的值,但它作为订户对象出现,我无法访问它

login.ts

  makeLoginRequest(form:any){
     this.authenticationMiddleWare.login(form).then(res => {
     console.log(" res", res)

     }).catch(err =>{
     console.log(" err", err)

     })

  }

service.ts

 async login(form:any):Promise<any>  {

    const options = this.loginRequestOptions();

    var result :any;

   result = await this.communication.request(
      this.httpModel.PAYLOAD = {
        needCache:true,
        queryString:"",
        params: {    
          Username: form.value.email,
          Password :form.value.password
        },
        httpRequest:"POST",
        responseType: "json",
        api: "/api/v0/usersaccount/",
        endpoint: "login",
        headers: options

    }).subscribe( (res:any) => {
      result = res // =>   I WANT THIS RESULT ON LOGIN

    }, (err:any) =>{
      result = err

    }) 

    return await result;

  }

我知道返回值,但是我不知道如何发送res值以登录。

1 个答案:

答案 0 :(得分:1)

只需返回Observable并在需要该结果的地方订阅,或者在需要结果的地方转换为Promise.then

return this.communication.request( // RETURN RIGHT AWAY
      this.httpModel.PAYLOAD = {
        needCache:true,
        queryString:"",
        params: {    
          Username: form.value.email,
          Password :form.value.password
        },
        httpRequest:"POST",
        responseType: "json",
        api: "/api/v0/usersaccount/",
        endpoint: "login",
        headers: options

    })