小程序常用的api记录

弹窗

消息提示——wx.showToast(OBJECT)

1
2
3
4
5
6
7
8
9
wx.showToast({
title: '数据发送成功',
icon: 'success',
})
wx.showToast({
title: '数据发送失败',
icon: 'none',
})

模态弹窗——wx.showModal(OBJECT)

1
2
3
4
5
6
7
8
9
10
11
12
类似于iOS系统里面的UIAlertView
wx.showModal({
title: '提示',
content: '这是一个模态弹窗',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})

操作菜单——wx.showActionSheet(OBJECT)

1
2
3
4
5
6
7
8
9
wx.showActionSheet({
itemList: ['A', 'B', 'C'],
success: function(res) {
console.log(res.tapIndex)
},
fail: function(res) {
console.log(res.errMsg)
}
})

指定modal弹出

指定哪个modal,可以通过hidden属性来进行选择。

1
2
3
4
5
6
7
<!--show.wxml-->
<view class="container" class="zn-uploadimg">
<button type="primary"bindtap="modalinput">modal有输入框</button>
</view>
<modal hidden="{{hiddenmodalput}}" title="请输入验证码" confirm-text="提交" cancel-text="重置" bindcancel="cancel" bindconfirm="confirm">
<input type='text'placeholder="请输入内容" auto-focus/>
</modal>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//show.js
//获取应用实例
var app = getApp()
Page({
data:{
hiddenmodalput:true,
//可以通过hidden是否掩藏弹出框的属性,来指定那个弹出框
},
//点击按钮痰喘指定的hiddenmodalput弹出框
modalinput:function(){
this.setData({
hiddenmodalput: !this.data.hiddenmodalput
})
},
//取消按钮
cancel: function(){
this.setData({
hiddenmodalput: true
});
},
//确认
confirm: function(){
this.setData({
hiddenmodalput: true
})
}
})

加载hudwx.showLoading(OBJECT)

显示loading提示框, 需主动调用 wx.hideLoading 才能关闭提示框

1
2
3
4
5
6
7
wx.showLoading({
title: '加载中',
})
setTimeout(function(){
wx.hideLoading()
},2000)