程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使 async await 在类中工作?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使 async await 在类中工作??

开发过程中遇到如何使 async await 在类中工作?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使 async await 在类中工作?的解决方法建议,希望对你解决如何使 async await 在类中工作?有所启发或帮助;

我正在尝试使异步等待函数在类中工作,但是当我的委托事件处理程序触发时,不知何故分配函数中的变量 this.shows 未定义。

this.shows.length in prevIoUsRangeShows() 返回 0,因为 this.shows 为空,但 getShows() 正确返回长度为 101 的节目。

export default class Shows {

  constructor() {
    console.log('Created Shows')
    this.RANGE = 4
    this.shows = []
    this.position = 0;
    shows.setupDelegatedEventHandlers()
  }

  async getShows() {
    console.log('GetTing shows from JsON...')
    this.shows = await $.getJsON('../Json/shows.Json')
    this.position = this.shows.length - this.RANGE
    this.renderSELEctionOfShows(this.position,this.shows.length)
    console.log('this.shows.length: ',this.shows.length)
  }

  nextRangeShows() {
    console.log('>>> next shows')
    if (this.position + 1 + this.RANGE < this.shows.length) {
      this.position += this.RANGE
      $('main').HTML = ''
      this.renderSELEctionOfShows(this.position,this.RANGE)
    }
  }

  prevIoUsRangeShows() {
    console.log('<<< prevIoUs shows')
    if (this.position > 0) {
      this.position -= this.RANGE
      $('main').HTML = ''
      this.renderSELEctionOfShows(this.position,this.RANGE)
    }
  }

  setupDelegatedEventHandlers() {
    $('main').on('click','#left-arrow',this.prevIoUsRangeShows)
    $('main').on('click','#right-arrow',this.nextRangeShows)
  }

  renderSELEctionOfShows(start,rangE) {
    $('main').append(`<img class="arrow" ID="left-arrow" src="../images/lefT_Bracket_white.png">`)
    for (let i = start; i < range; i++) {
      $('main').append(`<div ID="shows"><p>
        <strong>${this.shows[i].film}</strong><br>
        Saloon: ${this.shows[i].auditorium}<br>
        ${this.shows[i].datE} - ${this.shows[i].timE}
        </p></div>`)
    }
    $('main').append(`<img class="arrow" ID="right-arrow" src="../images/righT_Bracket_white.png">`)
  }

  renderAllShows() { //unused
    for (let show of this.shows) {
      $('main').append(`<p>
        MovIE: <strong> ${show.film} </strong><br>
        Saloon: ${show.auditorium}<br>
        Date: ${show.datE} - ${show.timE}
        </p>`)
    }

  }
}```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何使 async await 在类中工作?全部内容,希望文章能够帮你解决如何使 async await 在类中工作?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。