Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Json 获取可观察到的值<;任何>;输入到typescript中的数组中_Json_Angular_Typescript_Observable - Fatal编程技术网

Json 获取可观察到的值<;任何>;输入到typescript中的数组中

Json 获取可观察到的值<;任何>;输入到typescript中的数组中,json,angular,typescript,observable,Json,Angular,Typescript,Observable,我试图从typescript中的post中获取值,我从post操作中获取一个可观察的类型变量,但我不知道如何获取其中的值。有办法做到这一点吗 这是我到目前为止的代码 TryLogin() { this.loggedUser.email = this.inputUsername; this.loggedUser.password = this.inputPass; const myObjStr = JSON.stringify(this.loggedUser) co

我试图从typescript中的post中获取值,我从post操作中获取一个可观察的类型变量,但我不知道如何获取其中的值。有办法做到这一点吗

这是我到目前为止的代码

TryLogin() {
    this.loggedUser.email = this.inputUsername;
    this.loggedUser.password = this.inputPass;
    const myObjStr = JSON.stringify(this.loggedUser)
    const response = this.http.post<any>(this.url, myObjStr,{headers: this.httpheaders});
  }
TryLogin(){
this.loggedUser.email=this.inputUsername;
this.loggedUser.password=this.inputPass;
const myObjStr=JSON.stringify(this.loggedUser)
const response=this.http.post(this.url,myObjStr,{headers:this.httpheaders});
}

为什么不尝试订阅可观察对象并将结果分配给响应变量

this.http.post<any>(this.url, myObjStr,{headers: this.httpheaders}).subscribe(
result => {
// assigned to a class property response for convenience.
 this.response = result;
} );

this.http.post(this.url,myObjStr,{headers:this.httpheaders})。订阅(
结果=>{
//为方便起见,指定给类属性响应。
这个。反应=结果;
} );

请注意,可观察/主题是通知所有“订阅”它的东西(订阅者),其中的值已更改。

您需要订阅它。但是在你做这些之前,我建议你先阅读一下angular文档并熟悉它

TryLogin(){
this.loggedUser.email=this.inputUsername;
this.loggedUser.password=this.inputPass;
const myObjStr=JSON.stringify(this.loggedUser)
让userInfo;
this.http.post(this.url,myobjsr,{headers:this.httpheaders}).subscribe((response)=>{
//在这里对你的回答做点什么
userInfo=响应;
});
}

订阅吗?我强烈建议阅读这段代码是不正确的,除了获取一个值之外,不要做任何事情。不能像在代码中那样从subscribe()返回值。您需要将结果分配给subscribe中的另一个var,或者只需对其中的值执行一些操作。代码已更正。
TryLogin() {
    this.loggedUser.email = this.inputUsername;
    this.loggedUser.password = this.inputPass;
    const myObjStr = JSON.stringify(this.loggedUser)
    let userInfo;
    this.http.post<any>(this.url, myObjStr, { headers: this.httpheaders }).subscribe((response) => {
        // do something with your response here
        userInfo = response;
    });
}