如何正确的调用image的onload方法?
发布于 4 年前 作者 xieyang 8912 次浏览 来自 问答

我写了一个对image的封装,希望能够在image加载完毕后再进行渲染,但是这里有个问题就是我img的资源是通过wx,downloadfile下载后缓存的临时文件.我进行了如下调用,onload方法会被触发,this.parent输出的object的数据也是对的,但是对this.parent的任何属性修改,都不能实际影响this.parent,我打印restest对象的ready始终是fals

export  default class restest

{

 

constructor(resName) {

this.refCount = 0;

this.ready = false;

//check if exist in local file,if not download it

this.resName = resName;

this.Image = new Image;

}

    ActualLoad()

    {

        this.Image.src = this.resName;

        this.Image.parent = this

        this.Image.onload = function()

        {

            console.log(“image width” )

            console.log(this.parent)

            this.parent.ready = true

        }

    }

}

2 回复

这是代码逻辑问题,建议给个代码片段( https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html ),这样大家可以更容易帮助到你

问题找出来了,如果出现加载的图片不存在的话,onload也会被调用的,但是似乎这时的调用传入的参数和对象很奇怪

回到顶部