你瞅啥呢

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

直接看代码:

// 获取屏幕高度
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;
    }
});

如此即可。

posted @ 2023-06-21 09:36  叶乘风  阅读(268)  评论(0)    收藏  举报