此文档只是简单介绍,详细的开发以官方为准
获取网络状态
uni.getNetworkType({
success: (res) => {
console.log(res)
this.hasNetworkType = true, this.networkType = res.subtype || res.networkType
},
fail: () => {
uni.showModal({
content:'获取失败!',
showCancel:false
})
})
下拉刷新
1在pages.json中配置页面的信息开启下拉刷新
2在vue中添加方法
onPullDownRefresh() {
console.log('onPullDownRefresh');
this.initData();
},
3结束下拉加载方法
uni.stopPullDownRefresh();
上拉加载
1添加标签
<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
2在vue中添加方法
onLoad() {
this.initData();
},
onUnload() {
this.max = 0,
this.data = [],
this.loadMoreText = "加载更多",
this.showLoadMore = false;
},
onReachBottom() {
console.log("onReachBottom");
if (this.max > 40) {
this.loadMoreText = "没有更多数据了!"
return;
}
this.showLoadMore = true;
setTimeout(() => {
this.setListData();
}, 300);
},
网络请求
uni.request({
url: requestUrl,
dataType: 'text',
data: {
noncestr: Date.now()
},
success: (res) => {
console.log('request success', res)
uni.showToast({
title: '请求成功',
icon: 'success',
mask: true,
duration: duration
});
this.res = '请求结果 : ' + JSON.stringify(res);
},
fail: (err) => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
},
complete: () => {
this.loading = false;
}
});
loading框
显示:
uni.showLoading({
title: 'loading'
});
隐藏:
uni.hideLoading();
数据存储
同步方法
uni.getStorageSync(‘key’)
异步
获取:
uni.getStorage({
key: key,
success: (res) => {
uni.showModal({
title: '读取数据成功',
content: "data: '" + res.data +"'",
showCancel:false
}) },
fail: () => {
uni.showModal({
title: '读取数据失败',
content: "找不到 key 对应的数据",
showCancel:false
})
}
})
存储:
uni.setStorage({
key: key,
data: data,
success: (res) => {
uni.showModal({
title: '存储数据成功',
content: ' ',
showCancel:false
})
},
fail: () => {
uni.showModal({
title: '储存数据失败!',
showCancel:false
})
}
})
Toast提示消息
uni.showToast({
title: "显示一段轻提示",
position:'bottom'
})
震动
uni.vibrateLong({
success: function() {
console.log('success');
}
});
Form表单
<template>
<view>
<page-head title="form"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<form @submit="formSubmit" @reset="formReset">
<view class="uni-form-item uni-column">
<view class="title">姓名</view>
<input class="uni-input" name="nickname" placeholder="请输入姓名" />
</view>
<view class="uni-form-item uni-column">
<view class="title">性别</view>
<radio-group name="gender">
<label>
<radio value="男" /><text>男</text>
</label>
<label>
<radio value="女" /><text>女</text>
</label>
</radio-group>
</view>
<view class="uni-form-item uni-column">
<view class="title">爱好</view>
<checkbox-group name="loves">
<label>
<checkbox value="读书" /><text>读书</text>
</label>
<label>
<checkbox value="写字" /><text>写字</text>
</label>
</checkbox-group>
</view>
<view class="uni-form-item uni-column">
<view class="title">年龄</view>
<slider value="20" name="age" show-value></slider>
</view>
<view class="uni-form-item uni-column">
<view class="title">保留选项</view>
<view>
<switch name="switch" />
</view>
</view>
<view class="uni-btn-v">
<button form-type="submit">Submit</button>
<button type="default" form-type="reset">Reset</button>
</view>
</form>
</view>
</view>
</template>
<script>
var graceChecker = require("../../../common/graceChecker.js");
export default {
data() {
return {
}
},
methods: {
formSubmit: function(e) {
console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
//定义表单规则
var rule = [
{name:"nickname", checkType : "string", checkRule:"1,3", errorMsg:"姓名应为1-3个字符"},
{name:"gender", checkType : "in", checkRule:"男,女", errorMsg:"请选择性别"},
{name:"loves", checkType : "notnull", checkRule:"", errorMsg:"请选择爱好"}
];
//进行表单检查
var formData = e.detail.value;
var checkRes = graceChecker.check(formData, rule);
if(checkRes){
uni.showToast({title:"验证通过!", icon:"none"});
}else{
uni.showToast({ title: graceChecker.error, icon: "none" });
}
},
formReset: function(e) {
console.log('清空数据')
}
}
}
</script>
<style>
.uni-form-item .title {
padding: 20rpx 0;
}
</style>
选择框
<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
<view class="uni-input">{{array[index].name}}</view>
</picker>
array: [{name:'中国'},{name: '美国'}, {name:'巴西'}, {name:'日本'}],
switch按钮
<switch checked @change="switch1Change" />
推送
监听消息:
listenTranMsg() {
// IOS端在客户端在运行时收到推送消息触发receive事件,离线接收到
//的推送消息全部进入系统消息中心。点击消息中心的消息触发click
plus.push.addEventListener('click', (msg)=> {
this.tranMsg = JSON.stringify(msg)
});
plus.push.addEventListener('receive',(msg)=>{
this.tranMsg = JSON.stringify(msg)
})
uni.showToast({
title: '开始监听透传数据',
icon: 'success'
})
},
用浏览器打开url地址
plus.runtime.openURL(url);
配置底部tab
在pages.json中配置底部选项卡
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#007AFF",
"borderStyle": "black",
"backgroundColor": "#F8F8F8",
"list": [{
"pagePath": "pages/tabBar/component/component",
"iconPath": "static/component.png",
"selectedIconPath": "static/componentHL.png",
"text": "内置组件"
},
{
"pagePath": "pages/tabBar/API/API",
"iconPath": "static/api.png",
"selectedIconPath": "static/apiHL.png",
"text": "接口"
}, {
"pagePath": "pages/tabBar/extUI/extUI",
"iconPath": "static/extui.png",
"selectedIconPath": "static/extuiHL.png",
"text": "扩展组件"
}, {
"pagePath": "pages/tabBar/template/template",
"iconPath": "static/template.png",
"selectedIconPath": "static/templateHL.png",
"text": "模板"
}
]
}
页面跳转
保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack
可以返回到原页面。
页面跳转动画
uni.navigateTo({
url: '../test/test',
animationType: 'pop-in',
animationDuration: 200
});