Javascript 为什么订阅了很多次之后还要工作?

Javascript 为什么订阅了很多次之后还要工作?,javascript,angular,typescript,rxjs,behaviorsubject,Javascript,Angular,Typescript,Rxjs,Behaviorsubject,我有三个组件 发票列表组件 CreateinvoiceComponent 人头部 InvoicelistComponent具有到页面CreateInvoice的功能路由 getInvoiceDetail(invoiceId: number) { this.router.navigate('mainmenu/invoice/createinvoice', invoiceId) } 在Ngonit中创建InvoiceComponent获取invoiceId,从api获取数据并将数据

我有三个组件

  • 发票列表组件
  • CreateinvoiceComponent
  • 人头部
  • InvoicelistComponent具有到页面CreateInvoice的功能路由

      getInvoiceDetail(invoiceId: number) {
        this.router.navigate('mainmenu/invoice/createinvoice', invoiceId)
      }
    
    在Ngonit中创建InvoiceComponent获取invoiceId,从api获取数据并将数据设置为服务

      ngOnInit() {
        let checkMode = this.routerService.data === undefined || null ? false : true;
        if (checkMode === true) {
          this.spinner.show();
          let invoiceId = this.routerService.data;
          this.createInvoiceService.getdatainvoicebyid(invoiceId).subscribe(res => {
            let invoicemodel = new Invoice();
            invoicemodel = res;
            this.searchService.setdatainvoiceData(invoicemodel['result'][0]);
            this.spinner.hide();
          });
        }
      }
    
    SearchService具有函数setdatainvoiceData

      public setInvoiceData = new BehaviorSubject<any>(null);
      setdatainvoiceData(data: any) {
        this.setInvoiceData.next(data);
      }
    
    问题是当我第一次单击绿色图标时 在HeaderInvoiceComponent中订阅2轮后(在图中显示1轮,因为我检查空值)

    第1轮的值为空 第2轮的值不为空 也许我能在一轮内完成?有人能推荐吗

    当我第二次点击“返回页面”并点击绿色图标时,另一个项目

    为什么三轮工作


    您能否尝试将
    if(res!=null | | res!=undefined)
    更改为
    if(res)

    最佳做法是在导航离开当前页面时取消订阅以前的订阅。在每个组件中,尝试添加以下代码

    export class InvoicelistComponent implements OnInit, OnDestroy {
    
      destroy$: Subject<boolean> = new Subject<boolean>();
    
      constructor(
        private svc: ServiceName
      ) {}
    
      ngOnInit() {
        this.svc.setInvoiceData.pipe(
          takeUntil(this.destroy$)
        ).subscribe(res => {
          ...
        }); 
      }
      
     ngOnDestroy() {
       this.destroy$.next(true);
       this.destroy$.complete();
     }
    }
    
    导出类InvoicelistComponent实现OnInit、OnDestroy{
    销毁$:主题=新主题();
    建造师(
    私有svc:ServiceName
    ) {}
    恩戈尼尼特(){
    this.svc.setInvoiceData.pipe(
    takeUntil(此.destroy$)
    ).订阅(res=>{
    ...
    }); 
    }
    恩贡德斯特罗(){
    this.destroy$next(true);
    此.destroy$.complete();
    }
    }
    

    如果有帮助,请告诉我。

    我已经尝试过了,但没有效果。我不太确定,但我认为您需要订阅activatedRouter。paramMap:请参阅。您的
    路由服务是什么?注意:如果要使用状态传递参数(但在这种情况下,您需要使用navigateByUrl,请检查此链接:它可以工作!!谢谢,但它可以工作2轮。第1轮为空,第2轮为值。这是因为您使用的是BehaviorSubject。您可以将其改为Subject。默认情况下,BehaviorSubject会将初始数据直接发送给其订阅者。您会记录什么方法M另一种方法是什么?对于您的情况,只需使用如下主题。但是,我强烈建议使用ngxs之类的状态管理库
    public setInvoiceData=new Subject(null);
    export class InvoicelistComponent implements OnInit, OnDestroy {
    
      destroy$: Subject<boolean> = new Subject<boolean>();
    
      constructor(
        private svc: ServiceName
      ) {}
    
      ngOnInit() {
        this.svc.setInvoiceData.pipe(
          takeUntil(this.destroy$)
        ).subscribe(res => {
          ...
        }); 
      }
      
     ngOnDestroy() {
       this.destroy$.next(true);
       this.destroy$.complete();
     }
    }