首页 > 解决方案 > 通过服务设置值后偶尔会得到未定义

问题描述

我有公共领域在服务:

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

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

 // some code
}

事实上,这个字段的值是HeaderComponent通过订阅 other 来设置的revenueService

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
    }      
  }        
}

在这里,我不时得到undefinedfor this.shareDataService.templateForCurrencyCOGS以及 for 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]
     };
  }
}

我猜这个问题背后的原因是在为字段设置值RevenueAssistantComponent之前 initsHeaderComponent和解决方法是 在字段的设置值 之后始终RevenueAssistantComponent获取模板值。但我不知道如何实现它HeaderComponentshareDataService

标签: angulartypescript

解决方案


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

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

推荐阅读