在uniapp 开发中 当需要在获取缓存中的值时使用uni.getStorage()

代码如下:

uni.getStorage({
		key:keyword,
		success:function(res){
		    this.searchData = res.data
		}
	})
  console.log(this.searchData)

但是 你会发现 输出的结果是空的

正确的代码如下:

var self = this
uni.getStorage({
		key:keyword,
		success:function(res){
				self.searchData = res.data
                                console.log(self.searchData,self)
				console.log(this)
				}
	})

因为在这个api中 this的作用域发生了改变

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐