Skip to content

Uniapp

文档

引用文件

弹框

Toast

js
uni.showToast({
	title: '标题',
	icon: 'none',	//success:7个汉字,error:7个汉字,loading:7个汉字,none
	duration: 2000,
	position: 'center', //top:居上显示,bottom:居底显示,center
});

//隐藏Toast
uni.hideToast();

Loading

js
uni.showLoading({
	title: '加载中',
	mask: true
});

//隐藏Loading
uni.hideLoading();
js
uni.showModal({
	title: '提示',
	content: '这是一个模态弹窗',
	showCancel: true,
	cancelText: '取消',
	confirmText: '确定',
	success: function (res) {
		if (res.confirm) {
			console.log('用户点击确定');
		} else if (res.cancel) {
			console.log('用户点击取消');
		}
	}
});

网络请求

发起请求

data 数据说明:最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:

  • 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18
  • 对于 POST 方法且 header['content-type']application/json 的数据,会进行 JSON 序列化。
  • 对于 POST 方法且 header['content-type']application/x-www-form-urlencoded 的数据,会将数据转换为 query string。
js
uni.request({
    method: 'GET',	//POST
    url: '/url', //仅为示例,并非真实接口地址。
    data: {
        username: 'joje'
    },
    //自定义请求头信息
    header: {
        //'content-type': 'application/json',
        'content-type': 'application/x-www-form-urlencoded'
    },
    success: (res) => {},
    fail: (err)=> {},
    complete: (res)=> {}
});

数据缓存

设置和读取缓存

参数说明

参数类型必填说明
keyString本地缓存中的指定的 key
dataAny需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象
js
//设置缓存
uni.setStorageSync(key, data);

//读取缓存
const value = uni.getStorageSync(key);

删除缓存

js
//根据key删除缓存
uni.removeStorageSync(key);

//删除所有缓存
uni.clearStorageSync();

设备相关

系统信息

示例

js
uni.getSystemInfo({
	success: function (res) {
		console.log(res.model);
		console.log(res.pixelRatio);
		console.log(res.windowWidth);
		console.log(res.windowHeight);
		console.log(res.language);
		console.log(res.version);
		console.log(res.platform);
	}
});

定位信息

获取位置信息

js
uni.getLocation({
	type: 'wgs84',	//gcj02:国测局坐标,wgs84:gps坐标
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
	},
    fail: function(err){console.log(err);}
});