在最近的uniapp社区中发现一个很多人没有解决的一个问题,就是报错:

reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught SyntaxError: Unexpected token ','

有时候可以有时候不行,并且报错就是一片空白,其实这个问题很好解决,就是uniapp的this指向问题,废话不多说直接放代码

ces(){
				// 把this给test变量
				let test=this
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
                //下面使用test变量就不会报错了
						test.longitude=res.longitude
						test.latitude=res.latitude
						console.log(test.gg)
						console.log('当前位置的经度:' + res.longitude);
						console.log('当前位置的纬度:' + res.latitude);
					}
				}); 
			}

由于在uni.getLocation或者别的uniapp提供的api里面无法直接访问this,所以test相当于是一个桥梁

Logo

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

更多推荐