如何使 asyn 像角度 7 中的 ajax 一样错误



在我的项目中,我有一个完整的日历,可以从数据库动态加载事件。当我尝试将事件添加到日历中时。但是由于我认为httpget方法同步问题,它对我不起作用。我无法解决问题。

 this.events = [];
    this.holidayList = [];
    //this._holidayService.getHolidayArray().then(events => { this.events = events; });
    this._holidayService.getHolidayArray().subscribe((result: any[]) => {
      for (let i = 0; i < result.length; i++) {
        this.events.push({ "title": result[i].title, "start": result[i].start });
        //this.holidayModel = new Holiday;
        //this.holidayModel.start = result[i].start;
        //this.holidayModel.title = result[i].title;
        //this.holidayList.push(this.holidayModel);
      }
      //this.events = this.holidayList;
    });

 this.options = {
      plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
      selectable: true,
      selectMirror: true,
      select: function (arg) {
        var start = arg.start.toString();
          var date = start.slice(8, 10);
          var year = start.slice(11, 15);
          var month = start.slice(4, 7);
          if (month === "Jan") month = '01';
          if (month === "Feb") month = '02';
          if (month === "Mar") month = '03';
          if (month === "Apr") month = '04';
          if (month === "May") month = '05';
          if (month === "Jun") month = '06';
          if (month === 'Jul') month = '07';
          if (month === 'Aug') month = '08';
          if (month === 'Sep') month = '09';
          if (month === 'Oct') month = '10';
          if (month === 'Nov') month = '11';
        if (month === 'Dec') month = '12';
        var fullDate = month + '/' + date + '/' + year;
          $("#holiday").val(fullDate);
          document.getElementById("btnCreate").innerHTML = "Save";
          document.getElementById("description").innerHTML = "";
          $('#description').val("");
          $("#toggler")[0].click();
          //calendar.unselect()
       },
      defaultDate: '2019-07-01',
      header: {
        left: 'prev,next',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
      },
      events: this.events
    }
  }

  getHolidayArray() {
    return this.ExecuteGet("api/HolidayCalendar/GetTitles");
  }

如果我尝试

this._holidayService.getHolidayArray((.then(events => { this.events = 事件;});

错误"然后不存在可观察的类型">

请参阅您发布的链接(https://www.primefaces.org/primeng/#/fullcalendar(的"更改检测"部分。您不能只推送到事件数组中。相反,请尝试在循环后重新创建this.events

this._holidayService.getHolidayArray().subscribe((result: any[]) => {
  const events = [];
  for (let i = 0; i < result.length; i++) {
    events.push({ "title": result[i].title, "start": result[i].start });
  }
  this.events = [...this.events, ...events];
});

假设您使用的是 RxJS 5.5 或更高版本。

试一试:

getHolidayArray() {
  return this.ExecuteGet("api/HolidayCalendar/GetTitles");
}
----
import { map } from 'rxjs/operators';
this.events = [];
this.holidayList = [];
this._holidayService.getHolidayArray()
  .pipe(
    map((result: any[]) => result.map(item => ({
      title: item.title,
      start: item.start
    })))
  )
  .subscribe((result: any[]) => this.events = result);

相关内容

最新更新