通过服务设置值后,有时会变得不确定

时间:2019-07-03 11:06:15

标签: angular typescript

我在服务公共领域:

@Injectable()
export class ShareDataService {
  // some code

    public templateForCurrencyCOS;
    public templateForPercentCOS;
    public templateForCurrencyCOGS;
    public templateForPercentCOGS;

 // some code
}

实际上,通过订阅其他HeaderComponentrevenueService中设置了该字段的值:

export class HeaderComponent implements OnInit, OnDestroy {
  constructor(
    private revenueService: RevenueService
  ) {}

  ngOnInit() {
    this.addCOSItemCurrencyTemplate();
    this.addCOSItemPercentTemplate();
    this.addCOGSItemCurrencyTemplate();
    this.addCOGSItemPercentTemplate();
  }

  addCOSItemCurrencyTemplate() {
    if (!this.shareDataService.templateForCurrencyCOS) { 
      this.revenueService.addCostOfSellingItemCurrencyNew().subscribe( 
        response => {
          this.shareDataService.templateForCurrencyCOS = response;
        }, error => {
          console.log(error);
        });
      }
    }

    addCOSItemPercentTemplate() {
      if (!this.shareDataService.templateForPercentCOS) {
        this.revenueService.addCOSPercentNew().subscribe(response =>{ 
          this.shareDataService.templateForPercentCOS = response;
      }, error => {
        console.log(error);
      });
    }

    addCOGSItemCurrencyTemplate() {
      // similar implementation like addCOSItemPercentTemplate and addCOSItemCurrencyTemplate
    }

    addCOGSItemPercentTemplate() {
      // similar implementation like addCOSItemPercentTemplate and addCOSItemCurrencyTemplate
    }      
  }        
}

有时在这里,我得到undefined    this.shareDataService.templateForCurrencyCOGS以及    this.shareDataService.templateForCurrencyCOS,    this.shareDataService.templateForPercentCOGS,    this.shareDataService.templateForPercentCOS

export class RevenueAssistantComponent implements OnInit {
  ngOnInit() {

    this.templateForCurrencyCOGS = {...
      this.shareDataService.templateForCurrencyCOGS,
      fields: [...this.shareDataService.templateForCurrencyCOGS.fields]
    };

     this.templateForPercentCOGS = {... 
       this.shareDataService.templateForPercentCOGS,
       fields: [...this.shareDataService.templateForPercentCOGS && this.shareDataService.templateForPercentCOGS.fields]
      };
      this.templateForCurrencyCOS = {
        ...this.shareDataService.templateForCurrencyCOS,
        fields: [...this.shareDataService.templateForCurrencyCOS && this.shareDataService.templateForCurrencyCOS.fields]
      };
      this.templateForPercentCOS = {
        ...this.shareDataService.templateForPercentCOS,
        fields: [...this.shareDataService.templateForPercentCOS && 
        this.shareDataService.templateForPercentCOS.fields]
     };
  }
}

我猜想RevenueAssistantComponentHeaderComponent设置字段值和解决方法之前是在RevenueAssistantComponent本身中解决此问题的原因是  在HeaderComponent字段的shareDataService设置值中,body { background: #c0c0c0; } #block_with_line { width: 100%; position: relative; height: 18px; background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAASAQMAAACgmSb/AAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAZQTFRFAAAA4ODg8vF4+wAAAAJ0Uk5TAP9bkSK1AAAAEElEQVR4nGNgQAMN6AIMDAAJpACBAQttyAAAAABJRU5ErkJggg=='); } #block_with_line span { background: #e4e4e4; width: 150px; display: block; margin: 0 auto; position: absolute; left: 50%; margin-left: -75px; text-align: center }总是在之后获取模板值。但是我不知道如何实现

1 个答案:

答案 0 :(得分:0)

在声明时初始化将解决错误。

public templateForCurrencyCOS = {}; or templateForCurrencyCOS = []; // for Arrays
public templateForPercentCOS = {};
public templateForCurrencyCOGS = {};
public templateForPercentCOGS = {};