RxJs 6 ผูกมัดการสมัครสมาชิกหลายรายการ

ฉันมีแอป Angular 6 ฉันกำลังอัพเกรด rxjs เป็น 6

ในแอพเวอร์ชั่นก่อนๆผมต้องโทรแบบนี้ครับ...

ฉันเรียกสิ่งที่สังเกตได้ ทำบางสิ่งด้วยผลลัพธ์ของมัน จากนั้นเรียกสิ่งอื่นที่สังเกตได้โดยใช้ค่าจากครั้งล่าสุด ทำบางสิ่งด้วยผลลัพธ์ของมัน

ทำสิ่งนี้โดยใช้ concatmap รหัสมีลักษณะเช่นนี้ ...

this.service1.getData().concatMap(d1 => {
            this.d1Local = d1;
            return this.service2.getData(d1);
        }).subscribe(
            this.d2Local = d2,
            error => errorFunction);

ฉันกำลังพยายามเขียนสิ่งนี้ใหม่โดยใช้ rxjs6 และคีย์เวิร์ดไปป์และไม่มี concatmap

ขณะนี้ฉันมี...

this._LocationService.getLocation()
    .pipe(           
        map(location => {
            console.log(1);
            this.location = location;
            return this._GeoCoderService.getAddress(location)
                .pipe
                (map(address => {
                this.address = "You are near " + address;
                    this.showSpinner = false;
                }
                ))
            }
        )   
);

ฉันไม่เคยเห็นการบันทึก '1' ลงในคอนโซลเลย มีความคิดใดที่ฉันควรทำสิ่งนี้?


อัปเดต:

ฉันรู้ว่ามีรหัสด้านล่าง ฉันคิดว่ามันเกือบจะถูกต้องแล้วที่ฉันไม่เห็นข้อความ condole.log ล่าสุดที่พิมพ์ 1....

this._LocationService.getLocation()
    .pipe(map((location: ILocation) => {
        this.location = location;
        return this._GeoCoderService.getAddress(location);
    })
        , catchError(error => { this.showSpinner = false; return throwError('Something went wrong!') })
    ).subscribe(
        tap((address: string) => { console.log(1) })
    );

ฉันพลาดอะไรไป?


person Ben Cameron    schedule 11.07.2018    source แหล่งที่มา
comment
หาก this._GeoCoderService.getAddress ส่งคืน Observable คุณต้องใช้ concatMap (หรือ mergeMap ในกรณีนี้) คุณต้องสมัครสมาชิกเมื่อสิ้นสุดเครือข่ายด้วย   -  person martin    schedule 11.07.2018
comment
ทำไมคุณถึงทำ .tap() ภายใน subscribe()?   -  person CozyAzure    schedule 12.07.2018
comment
คุณไม่ควร tapใน subscribe - แค่ subscribe((address: string) => console.log(1));   -  person dsych    schedule 12.07.2018


คำตอบ (2)


ฉันจะไม่กำจัดตัวดำเนินการ concatMap เหมาะสมกับงานที่สุด เพื่อให้สิ่งต่าง ๆ สะอาดขึ้น ฉันจะไม่เก็บข้อมูลไว้ภายในตัวดำเนินการ map เช่นกัน สำหรับผลข้างเคียง tap เป็นตัวเลือกที่ดีที่สุดสำหรับโค้ดที่อ่านได้

this.service1.getData().pipe(
        tap(d1 => this.d1Local = d1),
        concatMap(d1 => this.service2.getData(d1),
    ).subscribe(
        d2 => this.d2Local = d2,
        error => errorFunction);
person kvetis    schedule 11.07.2018
comment
Concatmap ถูกลบออกจาก rxjs6 แล้ว ดังนั้นฉันต้องการดึงมันออกจากโค้ดของฉัน - person Ben Cameron; 11.07.2018
comment
ไม่ concatMap ไม่ได้ถูกลบออกจาก RxJS6 มันยังอยู่ที่นั่นใน rxjs/operators เป็นวิธีที่ง่ายที่สุดในการทำงานกับลำดับที่สังเกตได้สูงขึ้น ฉันสงสัยว่าโอเปอเรเตอร์ตระกูลนี้จะถูกลบออกจาก RxJS เลย - person kvetis; 13.07.2018

หลังจาก pipe() คุณควร .subscribe() (สิ่งที่สังเกตได้จะขี้เกียจ และถ้าคุณแค่ทำ pipe() มันจะไม่ทริกเกอร์ แต่ subscribe ทำ)

person dsych    schedule 11.07.2018