lucky-canvas icon indicating copy to clipboard operation
lucky-canvas copied to clipboard

lucky-canvas 获取不到 canvas 标签

Open dwfrost opened this issue 2 years ago • 3 comments

  • 你当前是什么框架(必填):uniapp

  • 你使用的是哪个包(必填): 使用HBuilderX导入插件,"version": "1.7.19"

  • 你当前插件的版本(必填):"version": "1.7.19"

  • 当前环境是小程序还是浏览器(选填):微信小程序

  • 详细描述你的bug:
  • 1.按文档进行引入,见文档
  • 2.组件无法渲染,报错lucky-canvas 获取不到 canvas 标签
  • 3.根据栈错误定位,发现源码抛出错误
          // #endif
          if (!res[0] || !res[0].node) return console.error('lucky-canvas 获取不到 canvas 标签')

tips:本项目是vite+vue3+uniapp+js的项目,见Vue3/Vite版

dwfrost avatar Feb 24 '22 07:02 dwfrost

在调试源码后,发现是初始化时未获取到,如

      initLucky () {
        this.boxWidth = changeUnits(this.width)
        this.boxHeight = changeUnits(this.height)
        this.isShow = true
        // 某些情况下获取不到 canvas
        this.$nextTick(() => {
          setTimeout(() => {
            this.draw()
          })
        })
      },

加了点延时是可以的,如下

        setTimeout(() => {
          this.draw()
        }, 100)

dwfrost avatar Feb 24 '22 07:02 dwfrost

奇怪的问题, 不知道小程序的dom有没有onload回调, 这样堆延迟好像不太合理

buuing avatar Feb 24 '22 07:02 buuing

optionsAPI有onLoad,不过如果是setup就没了

dwfrost avatar Feb 24 '22 07:02 dwfrost