小程序提供了一些交互API,开发者可以通过这些API来与用户进行交互。以下是一些常用的小程序交互API示例:
- //显示模态框
- wx.showModal({
- title: '提示',
- content: '这是一个模态框',
- success(res) {
- if (res.confirm) {
- console.log('用户点击确定')
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- //显示消息提示框
- wx.showToast({
- title: '成功',
- icon: 'success',
- duration: 2000
- })
- //显示 loading 提示框
- wx.showLoading({
- title: '加载中'
- })
- //隐藏 loading 提示框
- wx.hideLoading()
- //显示操作菜单
- wx.showActionSheet({
- itemList: ['A', 'B', 'C'],
- success(res) {
- console.log(res.tapIndex)
- },
- fail(res) {
- console.log(res.errMsg)
- }
- })
- //打开新的页面
- wx.navigateTo({
- url: '/pages/index/index'
- })
- //关闭当前页面并返回上一页
- wx.navigateBack({
- delta: 1
- })
- //获取用户的位置信息
- wx.getLocation({
- type: 'wgs84',
- success(res) {
- const latitude = res.latitude
- const longitude = res.longitude
- const speed = res.speed
- const accuracy = res.accuracy
- }
- })
这些API可以帮助开发者实现小程序与用户的交互,提高小程序的用户体验。