1、全局参数使用场景:
请求的地址有共同的前缀,比如 http://168.6.6.6:8080,这时候可以在入口文件main.js中配置成全局的参数
// 方法:挂载url
Vue.prototype.serverUrl = "http://168.6.6.6:8080"; // 生产环境
2、使用全局参数方式:
直接在***.vue中this.serverUrl使用
uni.request({
url: this.serverUrl + '/list',
method: "POST",
...
3、全局方法使用场景:
记录用户信息的方法,在应用中多处使用,这时候可以在入口文件main.js中配置成全局方法
Vue.prototype.getGlobalUser = function(key) {
var userInfo = uni.getStorageSync("globalUser");
if (userInfo != null && userInfo != "" && userInfo != undefined) {
return userInfo;}
else {
return null;
}
}
4、使用全局方法的方式:
直接在***.vue中this.getGlobalUser使用
// 使用挂载方法获取用户数据
var userInfo = me.getGlobalUser("globalUser");
if (userInfo != null) {
me.userInfo = userInfo;
} else {
me.userIsLogin = false;
me.userInfo = {};
}