本库把 uni-app 的回调式 API 封装为 Promise。要求 node >= 14.18。
本库不支持 uni_modules。
安装
npm i @uni-helper/uni-promises用 yarn v2/v3 时,必须按 yarnrc 文档 把 nodeLinker 设为 node_modules。
用 pnpm 时,必须按 pnpm 文档 把 shamefully-hoist 设为 true。
API
本库导出以下 Promise 化 API。名单与 src/ 下的目录一一对应:
- addPhoneContact
- authorize
- canvasGetImageData
- canvasPutImageData
- canvasToTempFilePath
- checkIsSoterEnrolledInDevice
- checkIsSupportSoterAuthentication
- checkSession
- chooseAddress
- chooseFile
- chooseImage
- chooseInvoice
- chooseInvoiceTitle
- chooseLocation
- chooseMedia
- chooseMessageFile
- chooseVideo
- clearStorage
- closeBLEConnection
- closeBluetoothAdapter
- closePreviewImage
- closeSocket
- compressImage
- compressVideo
- configMTLS
- connectSocket
- createBLEConnection
- createCameraContext
- createCanvasContext
- createInnerAudioContext
- createLivePlayerContext
- createLivePusherContext
- createMapContext
- createMediaContainer
- createPushMessage
- createSelectorQuery
- createVideoContext
- downloadFile
- getApp
- getBLEDeviceCharacteristics
- getBLEDeviceRSSI
- getBLEDeviceServices
- getBackgroundAudioManager
- getBatteryInfo
- getBeacons
- getBluetoothAdapterState
- getBluetoothDevices
- getCheckBoxState
- getClipboardData
- getConnectedBluetoothDevices
- getExtConfig
- getFileInfo
- getImageInfo
- getLocation
- getNetworkType
- getProvider
- getPushClientId
- getRecordManager
- getSavedFileInfo
- getSavedFileList
- getScreenBrightness
- getSelectedTextRange
- getSetting
- getStorage
- getStorageInfo
- getSystemInfo
- getUserInfo
- getUserProfile
- getVideoInfo
- hideHomeButton
- hideNavigationBarLoading
- hideShareMenu
- hideTabBar
- hideTabBarRedDot
- index.ts
- loadFontFace
- login
- makePhoneCall
- navigateBack
- navigateBackMiniProgram
- navigateTo
- navigateToMiniProgram
- notifyBLECharacteristicValueChange
- openAppAuthorizeSetting
- openBluetoothAdapter
- openDocument
- openLocation
- openSetting
- openVideoEditor
- pageScrollTo
- preLogin
- previewImage
- reLaunch
- readBLECharacteristicValue
- redirectTo
- removeSavedFile
- removeStorage
- removeTabBarBadge
- request
- requestMerchantTransfer
- requestPayment
- requestSubscribeMessage
- requestVirtualPayment
- saveFile
- saveImageToPhotosAlbum
- saveVideoToPhotosAlbum
- scanCode
- sendSocketMessage
- setBLEMTU
- setBackgroundColor
- setBackgroundTextStyle
- setClipboardData
- setEnableDebug
- setKeepScreenOn
- setNavigationBarColor
- setNavigationBarTitle
- setScreenBrightness
- setStorage
- setTabBarBadge
- setTabBarItem
- setTabBarStyle
- share
- shareWithSystem
- showActionSheet
- showLoading
- showModal
- showNavigationBarLoading
- showShareMenu
- showTabBar
- showTabBarRedDot
- showToast
- startAccelerometer
- startBeaconDiscovery
- startBluetoothDevicesDiscovery
- startCompass
- startFacialRecognitionVerify
- startGyroscope
- startLocationUpdate
- startLocationUpdateBackground
- startPullDownRefresh
- startSoterAuthentication
- stopAccelerometer
- stopBeaconDiscovery
- stopBluetoothDevicesDiscovery
- stopCompass
- stopGyroscope
- stopLocationUpdate
- switchTab
- types.ts
- uploadFile
- vibrate
- vibrateLong
- vibrateShort
- writeBLECharacteristicValue
构建
@uni-helper/uni-promises 用 unbuild 把 uni API 之外的部分转译到 ES2017(即 ES8)。uni API 必须由 uni-app 官方插件在项目构建时处理。
vue-cli + vue2
修改项目根目录的 vue.config.js,让 vue-cli 处理本库,保证生成代码符合 browserslist 里的配置。建议把 browserslist 设为 chrome>=53,ios>=8。
module.exports = {
transpileDependencies: ['@uni-helper/uni-promises'],
}
vite + vue3
先把 build.target 设为 ES6。
import uni from '@dcloudio/vite-plugin-uni'
import { defineConfig } from 'vite'
// https://vitejs.dev/config/
export default defineConfig({
build: {
target: 'es6',
cssTarget: 'chrome61', // https://cn.vitejs.dev/config/build-options.html#build-csstarget
},
plugins: [
// ...
uni(),
// ...
],
})
然后在 src/main.ts 或 src/main.js 里自行添加 polyfill。下面是使用 core-js 的示例,需要自行安装 core-js。也可以使用 es-shims。
// 按需添加额外的 polyfill
// import 'core-js/actual/object/values'
import { createSSRApp } from 'vue'
import App from './App.vue'
import 'core-js/actual/array/iterator'
import 'core-js/actual/promise'
import 'core-js/actual/object/assign'
import 'core-js/actual/promise/finally'
export function createApp() {
const app = createSSRApp(App)
return {
app,
}
}
微信小程序的 JavaScript 支持度见 wechat-miniprogram/miniprogram-compat。
微信小程序要支持 vue3,基础库最低版本必须是 2.11.2 或以上。2.11.2 对应 chrome>=53,ios>=10。
与官方类型定义的关系
@dcloudio/types@3.3.0 起提供基于 Promise 的类型定义,但不能同时正确支持 Vue 2 与 Vue 3,需要手动适配。
本库基于回调方法封装 Promise,同时支持 Vue 2 与 Vue 3。在官方类型定义覆盖本库的功能之前,本库继续维护。
致谢
本页目录