如何设置全局变量以包含从方法-Angular 6服务返回的数据?

时间:2019-05-02 04:39:34

标签: angular typescript dependency-injection

我正在尝试在我的服务中设置一个全局变量,以包含从数据库返回的对象数组,以允许服务中的方法访问该数组。

当前,我正在将使用HTTP GET方法从我的数据库中检索500多个对象的方法返回的内容记录到控制台。

我试图将GET方法返回的内容存储到变量中,然后可以将其传递给其他方法以进一步处理数据。

我无法在Google中正确表达我的问题,因此不确定我是否正确提出了问题。

这是我的代码...

const eventData = [];

@Injectable()
export class MonitoredEventService {

    constructor(private httpClient: HttpClient, private config: Config, private page: Page) {

    /*
    This is the original method I used to just get the events from the database
    */    
    }
    getMonitoredEvents2(batchSize, batchNumber) {
        return this.httpClient.get<MonitoredEvents>(this.config.getBaseUrl() + "/api/Events/" + batchSize + "/" +  batchNumber);
    }

    public getEvents(page: Page): Observable<PagedData<MonitoredEvents>> {
        return of(eventData).pipe(map(data => this.getPagedData(page)))
    }


    getMonitoredEvents(page: Page) {
        var batchSize = this.page.size;
        var batchNumber = this.page.pageNumber;
        var eventData =  this.httpClient.get<MonitoredEvents>(this.config.getBaseUrl() + "/api/Events/" + batchSize + "/" +  batchNumber);
        return eventData;
    }


    private getPagedData(page: Page): PagedData<MonitoredEvents> {
        const pagedData = new PagedData<MonitoredEvents>();
        page.totalElements = eventData.length;
        page.totalPages = page.totalElements / page.size;
        const start = page.pageNumber * page.size;
        const end = Math.min((start + page.size), page.totalElements);
        for (let i = start; i < end; i++){
            const eventObj = eventData[i];
            const event = new MonitoredEvents(eventObj.Id,
                eventObj.Venue,
                eventObj.Event,
                eventObj.Section,
                eventObj.EventDate,
                eventObj.Time,
                eventObj.TotalOrder,
                eventObj.TLC,
                eventObj.TicketType,
                eventObj.Source,
                eventObj.TicketPage,
                eventObj.NumberOfSeats,
                eventObj.SoldOut,
                );
            pagedData.data.push(event);
        }
        pagedData.page = page;
        return pagedData;
    }

}

Upload.C​​omponent.Ts代码:

ngOnInit() {
    this.getEvents(this.page);
    console.log(this.page.size);
    console.log(this.page.pageNumber);

    this.getEvents2(13,6);
    this.getEvents3(this.page);



    this.setPage({ offset: 0 });
}

和我的方法

getEvents(page: Page)
  {
    this.monitoredEventService.getMonitoredEvents(this.page).subscribe(res => {
        console.log(res);
    });
  }

  getEvents2(batchSize, batchNumber)
  {
    this.monitoredEventService.getMonitoredEvents2(batchSize, batchNumber).subscribe(res => {
      console.log(res);
    });
  }

  getEvents3(page: Page){
    this.monitoredEventService.getEvents(this.page).subscribe(res => {
      console.log(res);
    });
  }

  setPage(pageInfo){
    this.page.pageNumber = pageInfo.offset;
    this.monitoredEventService.getEvents(this.page).subscribe(pagedData => {
      console.log(pagedData);
      this.page = pagedData.page;
      this.rows = pagedData.data;
    });

    };

这也是我的控制台日志: enter image description here

每当我使用“ GetMonitoredEvents”方法登录到控制台时,我都可以看到所有返回到控制台的对象。

但是,当我登录“ GetEvents”时,我看到返回的数据数组为0 ...

1 个答案:

答案 0 :(得分:0)

您可以从const eventData = [];更改为static eventData = [];

但是通过这种方式,刷新页面时会重置变量。