Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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 订阅-角度2_Javascript_Angular - Fatal编程技术网

Javascript 订阅-角度2

Javascript 订阅-角度2,javascript,angular,Javascript,Angular,我有一个文件,我在其中编码了我与REST服务的整个连接,它可以正常工作 从另一个文件中,我正在执行以下行,一切正常 为了访问响应的值,我使用了HTML。例如:*ngIf=汽车 现在,我想通过Javascript访问执行以下操作的变量: this.baseService.getCars(ID) .subscribe(cars=> this.cars= cars); console.log(this.cars) 但我没有定义,但我可以通过HTML访问。我知道这是一个很难回答

我有一个文件,我在其中编码了我与REST服务的整个连接,它可以正常工作

从另一个文件中,我正在执行以下行,一切正常

为了访问响应的值,我使用了HTML。例如:*ngIf=汽车

现在,我想通过Javascript访问执行以下操作的变量:

this.baseService.getCars(ID)
        .subscribe(cars=> this.cars= cars);
console.log(this.cars) 

但我没有定义,但我可以通过HTML访问。我知道这是一个很难回答的问题,但我该怎么做呢?哪个变量包含该变量?

这些代码行的执行顺序与您认为的不同

要在控制台中查看车辆,请将您的功能更改为:

this.baseService.getCars(ID)
        .subscribe(cars=>{ 
    this.cars= cars;
    console.log(this.cars);
});

这些代码行的执行顺序与您认为的不同

要在控制台中查看车辆,请将您的功能更改为:

this.baseService.getCars(ID)
        .subscribe(cars=>{ 
    this.cars= cars;
    console.log(this.cars);
});

您需要将console.log放在subscribe中

this.baseService
    .getCars(ID)
    .subscribe(cars=> {
        this.cars = cars
        console.log(this.cars)
    });

您需要将console.log放在subscribe中

this.baseService
    .getCars(ID)
    .subscribe(cars=> {
        this.cars = cars
        console.log(this.cars)
    });

Subscribe是异步的,就像承诺一样,但不是承诺,因此,当您执行代码时,会触发Subscribe,然后触发控制台日志。但是,当console.log正在执行时,subscribe仍在运行,这就是为什么没有定义

您可以在subscribe中的回调函数中执行console.log

this.baseService
    .getCars(ID)
    .subscribe(cars=> {
        this.cars = cars
        console.log(this.cars)
    });
另一个解决方案是使用async/await。不能将async/wait直接用于subscribe,因为这不是一个承诺。幸运的是,观察者可以转化为承诺

因此,在您的服务中,您可以返回一个承诺,如下所示:

getCars() {
    // your service stuff
    return this.api.get(url).toPromise().then( res => res.data); // This is the important part.
}
然后,在您的组件中,使用async/await调用它:

async yourFunction() {
    this.cars = await this.baseService.getCars(ID);
    console.log(this.cars);
}
现在你可以在getCars之后记录这个


希望这对您有所帮助。

订阅是异步的,就像承诺一样,但不是承诺。因此,当您执行代码时,会触发订阅,然后触发控制台日志。但是,当console.log正在执行时,subscribe仍在运行,这就是为什么没有定义

您可以在subscribe中的回调函数中执行console.log

this.baseService
    .getCars(ID)
    .subscribe(cars=> {
        this.cars = cars
        console.log(this.cars)
    });
另一个解决方案是使用async/await。不能将async/wait直接用于subscribe,因为这不是一个承诺。幸运的是,观察者可以转化为承诺

因此,在您的服务中,您可以返回一个承诺,如下所示:

getCars() {
    // your service stuff
    return this.api.get(url).toPromise().then( res => res.data); // This is the important part.
}
然后,在您的组件中,使用async/await调用它:

async yourFunction() {
    this.cars = await this.baseService.getCars(ID);
    console.log(this.cars);
}
现在你可以在getCars之后记录这个


希望这对您有所帮助。

谢谢!我试过了,但我忘了{}谢谢!我试过了,但是我忘了{}正确答案在下面,但是你要明白,你没有看到这个值。因为你在订阅完成之前打印结果。我无法在10分钟之前检查答案是否正确正确正确正确答案在下面,但你要明白,您没有看到this.cars的值,因为您正在订阅完成之前打印结果。在10分钟之前,我无法检查答案是否正确