当前位置:   article > 正文

HarmonyOS的Swiper 数据为空导致页面崩溃的解决办法_foreach图片在swiper中预览器崩溃

foreach图片在swiper中预览器崩溃

当前: api9 DevEco Studio 3.1.1 Release

  1. @Entry
  2. @Component
  3. struct Index {
  4. // 定义接口返回的轮播图数据
  5. @State HospitalPropaganda: string[] = []
  6. private swiperController: SwiperController = new SwiperController()
  7. async aboutToAppear() {
  8. this.getOrgInfo()
  9. }
  10. private getOrgInfo = async () => {
  11. try {
  12. const res = await this.httpBaskData()
  13. this.HospitalPropaganda = res
  14. } catch (err) {
  15. console.log('err111', err)
  16. }
  17. }
  18. // 模拟接口返回数据
  19. private httpBaskData = (): Promise<string[]> => {
  20. return new Promise((res,rej) => {
  21. setTimeout(() => {
  22. res(['https://hwkjoss-kfx-biz-dev.oss-cn-hangzhou.aliyuncs.com/6395399560128654336.jpg', 'https://hwkjoss-kfx-biz-dev.oss-cn-hangzhou.aliyuncs.com/6395399560128654336.jpg'])
  23. },1000)
  24. })
  25. }
  26. build() {
  27. Column() {
  28. Text('this.HospitalPropaganda的数据是:-----')
  29. ForEach(this.HospitalPropaganda, (item: string) => {
  30. Text(item)
  31. })
  32. Text('Swiper:-----')
  33. // if (this.HospitalPropaganda.length > 0) {
  34. Swiper(this.swiperController) {
  35. ForEach(this.HospitalPropaganda, (item: string) => {
  36. Image(item) // https://hwkjoss-kfx-biz-dev.oss-cn-hangzhou.aliyuncs.com/6395399560128654336.jpg
  37. .width('100%').height('100%').objectFit(ImageFit.Fill)
  38. })
  39. // Image('https://hwkjoss-kfx-biz-dev.oss-cn-hangzhou.aliyuncs.com/6395399560128654336.jpg')
  40. }
  41. .width('100%')
  42. .height(100)
  43. .cachedCount(2)
  44. .index(1)
  45. .autoPlay(true)
  46. .interval(4000)
  47. .indicator(true)
  48. .loop(true)
  49. .duration(1000)
  50. .itemSpace(0)
  51. .curve(Curve.Linear)
  52. // }
  53. }
  54. .width('100%')
  55. .height('100%')
  56. .backgroundColor('#F7F7F7')
  57. }
  58. }

运行以上代码 预览器卡死;模拟器直接闪退;真机没有试过

已经反馈给官方:

解决办法:

在Swiper最外层加一个if判断

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/732366
推荐阅读
相关标签
  

闽ICP备14008679号