Ionic-native5.x版本以上处理装饰器,在npm run build:esm
的scripts命令里,有使用到scripts/build/transformers/plugin-class.ts
文件里的方法来处理,这个后面再来研究。先写4.x版本的。
新建项目
1 | npm install -g ionic |
将插件引入app.module.ts
文件的providers中。
业务模块内导入
页面
这边安装的@ionic-native与@ionic-native版本都是4.14.0
打包
连接手机
1 | npm install -g ios-deploy |
碰到第一个报错
1 | npm ERR! ios-deploy@1.9.4 preinstall: `./src/scripts/check_reqs.js && xcodebuild` |
解决方案:
1 | brew install ios-deploy |
第二个问题
打包失败,没有证书,打开xcode,打开platforms/ios/MyApp.codeproj
,配置证书,继续打包。
@ionic-native与@ionic-native/camera
@ionic-native/plugins/camera/index.ts
1 | import { Injectable } from '@angular/core'; |
主要看Plugin和Cordova装饰器
Plugin遍历了传入的config配置文件,为cls添加静态方法
1 | export function Plugin(config: PluginConfig): ClassDecorator { |
而Cordova装饰器,在调用方法的时候会调用到wrap方法
1 | export function Cordova(opts: CordovaOptions = {}) { |
调试
触发点击事件
获取插件实例
调用实例方法
postMessage通信
如果兼容多平台,可以考虑在get方法那配个映射表,修改获得的插件实例。