使用插件
小程序开发者可便捷地把插件添加到自己的小程序内, 丰富小程序的服务当用户在使用小程序时, 将可以在小程序内使用插件提供的服务
开放范围
所有小程序
接入流程
在小程序管理后台添加插件
小程序开发者可在小程序管理后台 - 设置 - 第三方服务 - 插件管理中, 根据 AppID 查找需要的插件, 并申请使用插件开发者在 24 小时内通过后, 小程序开发者可在小程序内使用该插件
在小程序代码中使用插件
引入插件代码包
对于插件的使用者, 使用插件前要在 app.json 中声明需要使用的插件, 例如:
- {
- "plugins": {
- "myPlugin": {
- "version": "1.0.0",
- "provider": "wxxxxxxxxxxxxxxxxx"
- }
- }
- }
如上例所示, plugins 定义段中可以包含多个插件声明, 每个插件声明中都必须指明插件的 appid 和需要使用的版本号
使用插件的 js 接口
在引入插件代码包之后, 就可以在这个小程序中使用插件提供的自定义组件或者 js 接口
如果需要使用插件的 js 接口, 可以使用 requirePlugin 方法:
- var myPluginInterface = requirePlugin(myPlugin)
- myPluginInterface.hello()
使用插件的自定义组件
使用插件提供的自定义组件, 和使用普通自定义组件的方式相仿在 json 文件定义需要引入的自定义组件时, 使用 plugin:// 协议即可, 例如:
- {
- "usingComponents": {
- "hello-component": "plugin://myPlugin/hello-component"
- }
- }
出于对插件的保护, 插件提供的自定义组件在使用上有一定的限制:
页面中的
this.selectComponent
接口无法获得插件的自定义组件实例对象;
wx.createSelectorQuery
等接口的 >>> 选择器无法选入插件内部
插件调用 API 的限制
插件可以调用的 API 与小程序不同, 主要有两个区别:
插件的请求域名列表与小程序相互独立;
一些 API 不允许插件调用
目前, 允许插件调用的 API 及其对应版本要求如下
API | 最低版本 |
---|---|
wx.addPhoneContact | 1.9.6 |
wx.arrayBufferToBase64 | 1.9.6 |
wx.base64ToArrayBuffer | 1.9.6 |
wx.canvasGetImageData | 1.9.6 |
wx.canvasPutImageData | 1.9.6 |
wx.canvasToTempFilePath | 1.9.6 |
wx.chooseImage | 1.9.6 |
wx.chooseLocation | 1.9.6 |
wx.chooseVideo | 1.9.6 |
wx.closeBLEConnection | 1.9.6 |
wx.closeBluetoothAdapter | 1.9.6 |
wx.connectSocket | 1.9.6 |
wx.createAnimation | 1.9.6 |
wx.createAudioContext | 1.9.6 |
wx.createBLEConnection | 1.9.6 |
wx.createCameraContext | 1.9.6 |
wx.createCanvasContext | 1.9.6 |
wx.createInnerAudioContext | 1.9.6 |
wx.createIntersectionObserver | 1.9.6 |
wx.createLivePlayerContext | 1.9.6 |
wx.createLivePusherContext | 1.9.6 |
wx.createMapContext | 1.9.6 |
wx.createSelectorQuery | 1.9.6 |
wx.createVideoContext | 1.9.6 |
wx.downloadFile | 1.9.6 |
wx.getBLEDeviceCharacteristics | 1.9.6 |
wx.getBLEDeviceServices | 1.9.6 |
wx.getBackgroundAudioManager | 1.9.6 |
wx.getBackgroundAudioPlayerState | 1.9.6 |
wx.getBeacons | 1.9.6 |
wx.getBluetoothAdapterState | 1.9.6 |
wx.getBluetoothDevices | 1.9.6 |
wx.getClipboardData | 1.9.6 |
wx.getConnectedBluetoothDevices | 1.9.6 |
wx.getImageInfo | 1.9.6 |
wx.getLocation | 1.9.6 |
wx.getNetworkType | 1.9.6 |
wx.getRecorderManager | 1.9.94 |
wx.getScreenBrightness | 1.9.6 |
wx.getStorage | 1.9.6 |
wx.getStorageSync | 1.9.6 |
wx.getSystemInfo | 1.9.6 |
wx.getSystemInfoSync | 1.9.6 |
wx.hideLoading | 1.9.6 |
wx.hideToast | 1.9.6 |
wx.makePhoneCall | 1.9.6 |
wx.makeVoIPCall | 1.9.6 |
wx.notifyBLECharacteristicValueChange | 1.9.6 |
wx.notifyBLECharacteristicValueChanged | 1.9.6 |
wx.onAccelerometerChange | 1.9.6 |
wx.onBLECharacteristicValueChange | 1.9.6 |
wx.onBLEConnectionStateChange | 1.9.6 |
wx.onBLEConnectionStateChanged | 1.9.6 |
wx.onBackgroundAudioPause | 1.9.6 |
wx.onBackgroundAudioPlay | 1.9.6 |
wx.onBackgroundAudioStop | 1.9.6 |
wx.onBeaconServiceChange | 1.9.6 |
wx.onBeaconUpdate | 1.9.6 |
wx.onBluetoothAdapterStateChange | 1.9.6 |
wx.onBluetoothDeviceFound | 1.9.6 |
wx.onCompassChange | 1.9.6 |
wx.onNetworkStatusChange | 1.9.6 |
wx.onUserCaptureScreen | 1.9.6 |
wx.openBluetoothAdapter | 1.9.6 |
wx.openLocation | 1.9.6 |
wx.pauseBackgroundAudio | 1.9.6 |
wx.pauseVoice | 1.9.6 |
wx.playBackgroundAudio | 1.9.6 |
wx.playVoice | 1.9.6 |
wx.previewImage | 1.9.6 |
wx.readBLECharacteristicValue | 1.9.6 |
wx.removeStorage | 1.9.6 |
wx.removeStorageSync | 1.9.6 |
wx.reportAnalytics | 1.9.6 |
wx.request | 1.9.6 |
wx.saveImageToPhotosAlbum | 1.9.6 |
wx.saveVideoToPhotosAlbum | 1.9.6 |
wx.scanCode | 1.9.6 |
wx.seekBackgroundAudio | 1.9.6 |
wx.setClipboardData | 1.9.6 |
wx.setKeepScreenOn | 1.9.6 |
wx.setScreenBrightness | 1.9.6 |
wx.setStorage | 1.9.6 |
wx.setStorageSync | 1.9.6 |
wx.showActionSheet | 1.9.6 |
wx.showLoading | 1.9.6 |
wx.showModal | 1.9.6 |
wx.showToast | 1.9.6 |
wx.startAccelerometer | 1.9.6 |
wx.startBeaconDiscovery | 1.9.6 |
wx.startBluetoothDevicesDiscovery | 1.9.6 |
wx.startCompass | 1.9.6 |
wx.startRecord | 1.9.6 |
wx.stopAccelerometer | 1.9.6 |
wx.stopBackgroundAudio | 1.9.6 |
wx.stopBeaconDiscovery | 1.9.6 |
wx.stopBluetoothDevicesDiscovery | 1.9.6 |
wx.stopCompass | 1.9.6 |
wx.stopRecord | 1.9.6 |
wx.stopVoice | 1.9.6 |
wx.uploadFile | 1.9.6 |
wx.vibrateLong | 1.9.6 |
wx.vibrateShort | 1.9.6 |
wx.writeBLECharacteristicValue | 1.9.6 |
来源: http://www.bubuko.com/infodetail-2526672.html