学习uni-app新手小白最容易犯的错误(一)
在uni-app中有很多像下面这样的API都是封装好的,对我们来说非常简单。
uni.getSystemInfo({
success: function(res) {
// console.log(res)
}
})
下面我说的这一点新手小白门往往都会踩的坑~~~~~~~~~~仔细看哦
下面代码就是在进入界面时获取你使用手机屏幕的代码,现在这里面是有错误的,这样使用是获取不到屏幕高度的,因为里面this指向改变了。(可以在代码中加入**console.log(this)**检查一下看看this返回是什么值)
onLoad() {
console.log(this)
uni.getSystemInfo({
console.log(this)
success: function(res) {
console.log(this)
// console.log(res)
this.windowHeight = res.windowHeight;
}
})
},
下面就给大家奉上解决方法
onLoad() {
let _this = this
uni.getSystemInfo({
success: function(res) {
// console.log(res)
_this.windowHeight = res.windowHeight;
}
})
},
在最外面吧this赋值给一个变量,在函数中直接用变量当做this使用