1. 更新Json数据
var self = this
self.setData({
hasLogin: true
})
that.update()
2. 网络请求
wx.request({
url: '',
data: {},
method: 'GET',
header: { 'content-type': 'application/json' },
success: function (res) {
var datas= res.data
},
fail: function () {},
complete: function () {}
})
3. 加载
//显示加载框
wx.showLoading({
title: '加载中',
})
//隐藏加载框
wx.hideLoading()
4. bindtap点击事件传值
data-tag自定义参数传参
<text data-tag="{{item.tag}}" id="{{item.tag}}" bindtap='checkPage'>{{item.value}}</text>
//实现
checkPage: function(event) {
var newTagId = event.target.id;
var tag = event.target.dataset.tag;
console.log("newTagId = " + newTagId)
console.log("tag = " + tag)
}
5. 跳转
wx.navigateTo({
url: './addresume/addresume?name=' + name + '&openId=' + openId,
})
wx.navigateTo({
url: '../welfare/addresume/addresume',
})
PS:注意url 两者的文件位置关系,以上两种写法都可达到目的
拓展分析:‘./’ 为当前目录下 ‘…/’则为当前目录的上级目录 ,’/'则为当前目录的下的某个目录
pages/welfare/welfare.js
pages/welfare/addresume/addresume.js
6. toast
wx.showToast({
title: '复制成功', duration: 1500,
})
7. 对话框
wx.showModal({
title: '复制该链接',
content: '完成后请手动打开浏览器,是否继续?' + link,
success: function (res) {
if (res.confirm) {
wx.setClipboardData({
data: link,
success: function () {
wx.showToast({
title: '复制成功',
duration: 1500,
})
},
fail: function () {
wx.showToast({
title: '复制失败',
icon: 'none',
duration: 500,
})
}
})
} else if (res.cancel) {
wx.showToast({
title: '取消复制',
icon: 'none',
duration: 500,
})
}
}
})
8. try catch
try {
//在这里运行代码
抛出错误
} catch(err) {
//在这里处理错误
console.log(err)
}
9. 获取用户信息
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
10. 打开附件
wx.downloadFile({ //下载附件
url: 'https://www.sz800800.cn/ZENGXING/0109...18.16.docx',
fileType:'docx',
success: function (res) {
console.log(res)
var filePath = res.tempFilePath
wx.openDocument({ //打开附件
filePath: filePath,
success: function (res) {
console.log(res)
},
complete:function(e){
console.log(e)
}
})
}
})
}
11. 打开文档
wx.showLoading({
title: '正在加载中...',
})
wx.downloadFile({
url: item.downLoadUrl,
success: function(res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function(res) {
wx.hideLoading()()
}
})
}
})
---------------
常用API
wx.canIUse(string schema)
判断小程序的API、回调、参数、组件等是否在当前版本可用
wx.getSystemInfoSync() | wx.getSystemInfo()
获取系统信息
wx.getLaunchOptionsSync()
获取小程序启动时的参数
属性
path:启动小程序的路径
scene:启动小程序的场景值
query:启动小程序的query参数
shareTicket:转发信息
referrerInfo:来源信息
应用级事件
wx.onPageNotFound(function callback) | App.onPageNotFound(function callback)
监听小程序要打开的页面不存在事件
属性
path:不存在页面的路径
query:不存在也秒的query参数
isEntryPage:时候本次启动的首个页面
wx.onError(function callback)
监听小程序错误事件
wx.onAudioInterruptionEnd(function callback)
监听音视频中断结束事件
wx.onAudioInterruptionBegin(function callback)
监听音频因系统占用而被中断开始事件(闹钟、电话、语音聊天、视频聊天等)
wx.onAppShow(function callback) | App.onShow(function callback)
监听小程序切前台事件
属性
path:小程序切前台的路径
scene: 小程序切前台的场景值
query:小程序切前台的query参数
ShareTicket:转发信息
referrerInfo:来源信息
wx.onAppHide | App.onHide
监听小程序切后台事件
wx.offPageNotFound(function callback) | wx.offError(function callback) | wx.offAudioInterruptionEnd(function callBack) | wx.offAudioInterruptionBegin(function callBack) | wx.offAppShow(function callBack) | wx.offAppHide(function callBack)
取消监听事件
定时器
clearInterval(interavalID)
取消setInterval设置的定时器
clearTimeout(timeoutID)
取消setTimeout 设置的定时器
setInterval(callback,number delay, any rest)
设置Interval定时器
setTimeout(callback,number delay, any rest)
设置timeout定时器
路由
wx.switchTab(object) I wx.reLaunch(object) | wx…redirect(object) | wx.navigateTo(object) | wx.navigateBack(object)
页面路由
参数:
url: 需要跳转的页面路径
success:调用成功的回调函数
fail:调用失败的回调函数
complete: 调用结束的回调函数
界面相关
wx.showToast(object) | wx.hideToast(object)
显示消息框
参数
title: 提示的内容
icon: 图标
image: 自定义图标的本地路径,优先级高于icon
duration: 提示的延迟时间
mask: 是否显示透明蒙层
success: 调用成功的回调函数
fail: 调用失败的回调函数
complete: 接口调用结束的回调函数
wx.showModal(object)
显示模态对话框
参数:
title: 提示的标题
content: 提示的内容
showCancel: 是否显示取消按钮
cancelText: 取消按钮文字
cancelColor: 取消按钮文字颜色
confirmTextL 确认按钮的文字
confirmColor: 确认按钮的文字颜色
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.showLoading(object) | wx.hideLoading(object)
显示loading提示框
参数
title: 提示的内容
mask: 是否显示透明蒙层
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.pageScrollTo(Object)
将页面滚动到目标位置
参数
scrollTop: 滚动到页面的目标位置
duration: 滚动动画的时长
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
网络相关
wx,request(object)
发起网络请求
参数
url: 服务器接口地址
data: 请求参数
header: 设置请求的header
method:HTTP请求方法
dataType: 返回的数据格式
responseType:响应的数据类型
success: 接口调用成功的回调函数
fail:接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.downloadFile(object)
下载文件资源到本地
参数
url:下载资源的url
header:HTTP请求的Header
filePath: 指定文件下载后存储的路径
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete:接口调用结束的回调函数
wx.uploadFile(object)
将本地资源上传到服务器
参数
url: 开发者服务器地址
filePath: 要上传文件资源的路径
name: 文件对应的key
header: HTTP请求的Header
formData: HTTP请求中其他额外的form data
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.connectSocket(object)
创建一个WebSocket连接
属性
url: 服务器wss接口地址
header: HTTP Header
protocols: 子协议数组
tcpNoDelay: 建立TCP 连接的时候的TCP_NODELAY设置
success: 接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.closeSocket(object)
关闭WebSocket 连接
参数
code: 连接状态号
reason:表示连接被关闭的原因
success:接口调用成功的回调函数
fail: 接口调用失败的回调函数
complete: 接口调用结束的回调函数
wx.onSocketMessage(function) | wx.onSocketError(object) | wx.onSocketClose(object) | wx.onSocketOpen(object)
监听webSocket 相关事件
本文详细介绍了微信小程序中常用API的使用,包括更新Json数据、网络请求、加载、事件处理、页面跳转、对话框、错误处理、获取用户信息、打开附件和文档等,并提供了相关API的使用示例和应用场景。
1177

被折叠的 条评论
为什么被折叠?



