首页 > 其他分享 >2023-06-21 vue 变量赋值失败

2023-06-21 vue 变量赋值失败

时间:2023-06-21 09:45:09浏览次数:56  
标签:vue 06 21 res getSystemInfo screHeight 赋值

直接看代码:

// 获取屏幕高度
uni.getSystemInfo({
    success: function (res) {
    this.screHeight = res.screenHeight;
    }
});

这个变量screHeight没有被赋值,拿到的还是我设置的初始值。

原因:this指向的作用域并不是Vue实例本身,所以就无法赋值。

解决方案:在最外一层绑定this的值,见下文:

let that = this;
// 获取屏幕高度
uni.getSystemInfo({
    success: function (res) {
    that.screHeight = res.screenHeight;
    }
});

如此即可。

标签:vue,06,21,res,getSystemInfo,screHeight,赋值
From: https://www.cnblogs.com/iuniko/p/17495473.html

相关文章