Vue中上传文件,使用fr.onload = function(){} vue无法进行数据绑定

Vue中上传文件,使用fr.onload = function(){} vue无法进行数据绑定

核心代码如下:

1 fr.onload = function () {
2     this.imageUrl = fr.result;
3     // console.log(this.imageUrl);
4 };

在执行完这个函数后,在外部打印this.imageUrl,发现无法获得预期的结果

仔细分析可知,这里的this,指向的是这个function()函数,不是全局的!

因此我们应该用箭头函数,修改代码如下:

1 fr.onload = () => {
2      this.imageUrl = fr.result;
3     // console.log(this.imageUrl);
4  };

猜你喜欢

转载自www.cnblogs.com/ABKing/p/12451795.html
今日推荐