uni-app 全局变量globalData的使用

1:在App.vue中

要这样使用 ,let _this = this

在内部函数onShow,onLaunch等生命周期函数中要这样:

wx.getStorage({
	key: 'phoneNumber',
	success(res) {
		_this.$options.globalData.obtainChatAllow = true;
		_this.$options.globalData.obtainLikeAllow = true;
		_this.$options.globalData.obtainCallAllow = true;
		_this.$options.globalData.obtainMailAllow = true;
		_this.$options.globalData.obtainPhoneAllow = true;
		_this.$options.globalData.userPhone = res.data;
	}
})

注意:在App.vue页面中的自定义函数中,如果加上$options会报错,所以要这样

	//微信app用户绑定
	bindThird: function (phone) {
	    const _this = this;
	    let options = {
	      url: "/applet/user/bindThird",
	      params: {
	        "phone": phone,
	      },
	      successFn(res) {
	        const data = res.data.data;
	        _this.globalData.userToken = data.yunxinToken;
	        _this.globalData.userAccId = data.accId;
	      },
	      failFn(err) {
	        _this.uni.toast("绑定用户失败,请重试");
	        console.log("绑定用户失败,请重试")
	      }
	    }
	    return request(options);
	},

2:在其它页面使用

import app from "../../App.vue"

created() {
	const _this = this;
	_this.dynamicIcon = app.globalData.headIcon;
}

官网没去描述,这个坑踩得

发布了149 篇原创文章 · 获赞 51 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/caoyan0829/article/details/99650521
今日推荐