Ionic Capacitor 插件开发
NPM 私有库搭建
因为我们很多时候可能并不想使用公有的npm库来保存我们的软件包,但在开发中npm包的方便性确需要我们能够快速安装我们自己和同事开发的软件包,而不需要使用代码拷贝的方式。我的选择也是如此,那如果搭建私有库呢,网上有许多方案,但是有一些非常好的的工具能让我们搭建私有库非常方便简单,以前有一个sinopia,但是作者不维护了,衍生出来的另一个神器,verdaccio(https://github.com/verdaccio/verdaccio)两步就能搞定:
- 安装
npm install –global verdaccio - 执行
verdaccio
Perfect,搭建完了。enjoy it
Ionic Capacitor 插件开发
npx @capacitor/cli plugin:generate ✏️ Creating new Capacitor plugin ? Plugin NPM name (snake-case): my-plugin ? Plugin id (domain-style syntax. ex: com.example.plugin) com.ionicframework.myplugin ? Plugin class name (ex: AwesomePlugin) MyPlugin ? description: ? git repository: ? author: ? license: MIT ? package.json will be created, do you want to continue? (Y/n)
完成后会产生一个插件目录
每个插件都带有一些TypeScript文件,这些文件仅导出TypeScript接口。这些接口可以为插件的TypeScript使用者提供键入。
从TypeScript接口开始可能是为插件构建API的好方法。例如,这是我们插件的默认界面:
declare module "@capacitor/core" { interface PluginRegistry { Echo: EchoPlugin; } } export interface EchoPlugin { echo(options: { value: string }): Promise<{value: string}>; }
发布插件
每当您准备发布插件时,只需使用:
npm publish
现在,可以npm install your-plugin在任何Capacitor应用程序中使用安装软件包了。
Ionic 的 capacitor构建插件真的比cordova简单方便多了。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216611.html原文链接:https://javaforall.net
