代码之家  ›  专栏  ›  技术社区  ›  Noober

CombineLast不是因为行为主题而激发的吗?

  •  0
  • Noober  · 技术社区  · 6 年前

    以下代码来自我的服务组件。它要求用户可以观察到,并在行为主题中提交数据。我打电话 form.service.formToSubmit$.next(form); 更新它。因为combinelatest也会在用户可观察到的发出时激发,所以我会检查formtoSubmit是否不为空,以及在提交之后是否设置为空。但当我更新formtoSubmit时它不会触发。我什么都试过了。

         this.formToSubmit$ = new BehaviorSubject<Forms>(null);  
        constructor(){
        this.updateForm();
    
    }
        updateForm(){
                combineLatest(this.authService.user$, this.formToSubmit$).pipe(
                    switchMap(([user, form]) =>{
                        if(form == null) return;
                        return this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('inspectionForms').doc(form.id).set(JSON.parse(JSON.stringify(form))).then(success=>{
                            this.formToSubmit$.next(null);
                        }).catch(err=>{
                            this.formToSubmit$.next(null);  
                        });
                    })
                );
            }
    

    这是以前的代码,工作得很好。

    updateFormdd(form: Forms){
            return Observable.create(observer=>{
                this.authService.user$.subscribe(user=>{
                    this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('inspectionForms').doc(form.id).set(JSON.parse(JSON.stringify(form))).then(success=>{
                        observer.next(success);
                        observer.complete();
                    }).catch(err=>{
                        observer.err(err);
                    });
                })
            })
        }
    

    但最终的目标是找出如何实现上述功能,但是允许我将最多3个可观察的功能结合起来,就像下面的功能一样。

      updateInspection(){
    
                combineLatest(this.authService.user$, this.equipmentId$, this.inspectionToSubmit$).pipe(
                    switchMap(([user, equipmentId, form]) =>{
                        if(form == null) return;
                        return this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('equipment').doc(equipmentId).set(JSON.parse(JSON.stringify(form))).then(success=>{
                            this.formToSubmit$.next(null);
                        }).catch(err=>{
                            this.formToSubmit$.next(null);
                        });
                    })
                );
    
        }
    

    我只是想确保我正确使用了Observable。谢谢你

    1 回复  |  直到 6 年前
        1
  •  0
  •   Juan M. Medina    6 年前

    两段代码之间的区别的关键是“订阅”。

    工作版本有订阅。因为你的可观测物是冷的可观测物,所以只有订阅了某些东西,它们才会发射出来。

    找出你想在哪里使用这个可观察的,然后在那里添加订阅。

    this.formToSubmit$ = new BehaviorSubject<Forms>(null);  
    constructor(){
    let mySubscription = this.updateForm().subscribe(
        (dataFromCombineLatest) => {
            // This subscription should cause the code inside the combine to fire
        }
    );
    

    另外,如果这是在“组件”中,我建议在“OnInit”中运行该位,而不是在构造函数中运行(我喜欢我的构造函数代码是瘦的)。