Angular6 我如何等待,直到我的观察完成服务调用?

Angular6 我如何等待,直到我的观察完成服务调用?,angular6,Angular6,我有以下代码。它调用Angular服务,Angular服务反过来调用REST服务从数据库获取数据 this.carService.getCarsById(id, searchType) .subscribe(c => { this.cars = c; }); tmpCars = this.cars.map(x => x.color); console.log("cars: " + tmpCars); 问题是在从数据库返回数据之前执行以tmpCars=和console.log

我有以下代码。它调用Angular服务,Angular服务反过来调用REST服务从数据库获取数据

this.carService.getCarsById(id, searchType)
.subscribe(c => {
  this.cars = c;
});

tmpCars = this.cars.map(x => x.color);

console.log("cars: " + tmpCars);

问题是在从数据库返回数据之前执行以
tmpCars=
console.log()开头的行。在可观察到的
getCarsById()
an
observable
返回所有数据之前,如何阻止代码继续进行

我不使用Angular,但它似乎是一种普通的发布/订阅机制

如果这个是异步的,并且tmpCars应该使用数据库中的数据进行更新,那么您应该在订阅回调上执行该逻辑

this.carService.getCarsById(id, searchType).subscribe(c => { //MY LOGIC FROM DB});

希望有帮助。

JS中的异步操作只能通过回调进行交互。如果要在订阅完成后执行任何操作,则需要在
subscribe
调用中将其添加为操作:

this.carService.getCarsById(id, searchType)
        .subscribe(c => {
            this.cars = c;
            const tmpCars = this.cars.map(x => x.color);
            console.log("cars: " + tmpCars);
        })
如果您希望整个页面真正停止,直到请求完成,那么您需要使您的请求成为同步(阻塞)请求。通常建议不要这样做,因为这会对页面性能产生负面影响。页面可能正在工作,但它只是坐在那里旋转,直到请求完成

默认情况下,Angular()中似乎不支持同步请求

或者,如果您希望在更新汽车时执行其他杂项操作,则您可以将“汽车”支架作为
主体
,作为可观察对象:

car.component.html:

<div *ngFor="let car of cars$ | async">
    <div>Car</div>
    <div>Make: {{car.make}}</div>
    <div>Model: {{car.model}}</div>
    <div>Year: {{car.year}}</div>
</div>
...
private carsSubject: Subject<Car[]> = new BehaviorSubject([]);
private cars$: Observable<Car[]> = this.carsSubject.asObservable();
private carsSubscription: Subscription;

constructor(private carService: CarService) {
    this.carsSubscription = this.cars$.subscribe(cars => {
        const tmpCars = cars.map(x => x.color);
        console.log("cars: "+ tmpCars);
    });
}

ngOnDestroy() {
    this.carsSubscription.unsubscribe();
}

getCars(id, searchType) {
    this.carService.getCarsById(id, searchType)
            .subscribe(cars => this.carsSubject.next(cars));
}
...

汽车
Make:{{car.Make}
模型:{{car.Model}
年份:{{car.Year}
car.component.ts:

<div *ngFor="let car of cars$ | async">
    <div>Car</div>
    <div>Make: {{car.make}}</div>
    <div>Model: {{car.model}}</div>
    <div>Year: {{car.year}}</div>
</div>
...
private carsSubject: Subject<Car[]> = new BehaviorSubject([]);
private cars$: Observable<Car[]> = this.carsSubject.asObservable();
private carsSubscription: Subscription;

constructor(private carService: CarService) {
    this.carsSubscription = this.cars$.subscribe(cars => {
        const tmpCars = cars.map(x => x.color);
        console.log("cars: "+ tmpCars);
    });
}

ngOnDestroy() {
    this.carsSubscription.unsubscribe();
}

getCars(id, searchType) {
    this.carService.getCarsById(id, searchType)
            .subscribe(cars => this.carsSubject.next(cars));
}
...
。。。
私家车主体:主体=新行为主体([]);
私家车$:Observable=this.carsObject.asObservable();
私家车认购:认购;
建造商(私人汽车服务:汽车服务){
this.carsSubscription=this.cars$.subscribe(cars=>{
const tmpCars=cars.map(x=>x.color);
控制台日志(“cars:+TMPCAR”);
});
}
恩贡德斯特罗(){
此.carsubscription.unsubscribe();
}
getCars(id,搜索类型){
this.carService.getCarsById(id,searchType)
.subscribe(cars=>this.carsObject.next(cars));
}
...
一些代码可以根据您的需要进行裁剪