【问题标题】:Clear static data on ngOnDestroy, after getting the data from service从服务获取数据后,清除 ngOnDestroy 上的静态数据
【发布时间】:2020-07-19 08:45:36
【问题描述】:

我想清除 ngOnDestroy 上的数据。我从服务中获取这些静态数据。 目前的情况是我使用相同的组件进行添加编辑,我只想在用户完成编辑后清除临时静态数据。

export class LeadsDefaultDataService {

tempData;

private leadsDefaults = {
leads: {
  data: [
    {
      type: 'page_title',
      data: {
        title: 'Enter data',
      }
    },
    {
      type: 'page_layout',
      data: {
        backgroundColor: '#FFFFFF'
      }
    }
}
};


/**
 * Constructor
 */
 constructor(
 ) {}

 /**
  *  Function used to return the default data of qrCategory
  * @param category: Name of the category
  */
  getLeadsDefaultData(category: string) {
   return this.leadsDefaults[category].data;
  }
}

在组件中:

constructor(
  private defaultData: LeadsDefaultDataService,
 ) {
}



   // For getting the data
   this.leadsData =  this.defaultData.getLeadsDefaultData('leads');


  // Reset the leadsData.
  ngOnDestroy() {
   this.leadsData = null; // not working....
  }

【问题讨论】:

    标签: javascript angular typescript rxjs angular-lifecycle-hooks


    【解决方案1】:

    您只是在使引用无效。作为一种更好的方法,您可以通过在服务中公开方法来做到这一点,如下所示

    export class LeadsDefaultDataService {
     .......
      setLeadData(category: string, value: any){
        this.leadsDefaults[category].data = value;
      }
    }
    

    并从组件调用此方法

     ngOnDestroy() {
        this.defaultData.setLeadData('leads', null);
      }
    

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 2012-10-22
      • 2013-01-23
      • 2018-01-01
      相关资源
      最近更新 更多