uniapp中的this指向哪里


this不指向组件,但无需关心他具体指向哪里,毕竟uniapp的网络请求是封装过后的api,只要确定拿到想要的this就好了。

比如可以直接在外部设置保存this的that,回调函数利用词法作用域使用that。

            console.log(this);
             //请求外的log指向组件
uni.request({
	url:'http://192.168.2.30:3000/app/friend/detail',
	header: {
		'content-type': 'application/json'
	},
	method: 'POST',
	data: {},
	success(res) {
                    // console.log(this);
                    //1指向回调函数的调用 ƒ success(_x) {return _success.apply(this, arguments);}
		//2指向回调函数体

		//反正不指向组件

		function a(){
			console.log(this);
		}
		a();
		//undefined 因为到这里的时候是第2种情况 外部this指向回调函数体
	},
	fail(res) {

	},
	complete(res) {

	}
});
//App._post_form中的this指向组件对象

文章作者: iamfugui
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iamfugui !
评论
  目录