调用风格
所有 woo.* 函数同时支持 Promise 和 回调 两种风格。
Promise 风格(推荐)
ts
try {
const result = await woo.getDeviceInfo()
console.log(result.model)
} catch (err) {
console.error(err.code, err.message)
}回调风格
ts
woo.getDeviceInfo({
success(res) {
console.log(res.model)
},
fail(err) {
console.error(err.code, err.message)
},
complete() {
console.log('请求完成')
},
})混用
ts
// success/fail/complete 回调与 await 可同时使用,行为相同
const result = await woo.getDeviceInfo({
success(res) {
console.log('callback:', res.model)
},
})
console.log('await:', result.model)字符串简写
部分 API 支持传入字符串作为第一个参数(等价于对象形式的常用字段):
ts
// 等价于 woo.showToast({ title: '成功', icon: 'none', duration: 2000 })
await woo.showToast('成功')
// 等价于 woo.navigateTo({ url: 'pages/detail/index' })
await woo.navigateTo('pages/detail/index')
// 等价于 woo.showLoading({ title: '加载中', mask: true })
await woo.showLoading('加载中')支持字符串简写的 API:
| API | 等价字段 |
|---|---|
showToast(str) | { title: str, icon: 'none', duration: 2000 } |
showLoading(str) | { title: str, mask: true } |
navigateTo(str) | { url: str } |
reLaunch(str) | { url: str } |
navigateToTab(str) | { url: str } |
reLaunchTab(str) | { url: str } |
alert(str) | { content: str, buttonText: 'OK' } |
通用回调接口
所有支持回调的 API 均遵循以下接口:
ts
interface ApiCallbacks<T = void> {
success?: (res: T) => void // 调用成功
fail?: (err: { code: number; message: string }) => void // 调用失败
complete?: () => void // 无论成功失败均触发
}