NativeScript - 插件



npm 包用于添加原生功能。使用此包,我们可以安装、搜索或删除任何插件。本节详细介绍了插件。

命令

add − 用于安装插件。

update − 更新指定的插件并修改其依赖项。

remove − 删除插件。

build − 用于为 iOS 或 Android 项目构建插件。

create − 为您的项目创建插件。

添加插件

以下语法用于添加新插件:

tns plugin add <plugin-name>

例如,如果要添加 nativescript-barcodescanner,可以使用以下代码:

tns plugin add nativescript-barcodescanner

您可能会看到以下响应:

+ [email protected] 
added 1 package from 1 contributor and audited 11704 packages in 8.76s

您也可以使用 npm 模块来添加上述插件:

npm install nativescript-barcodescanner

现在,NativeScript CLI 从 npm 下载插件并将其添加到您的 node_modules 文件夹中。

如果要将插件直接添加到您的 package.json 并解决所有依赖项问题,可以使用以下命令代替上一个命令:

npm i nativescript-barcodescanner

如果要在开发期间安装开发依赖项,请使用以下代码:

npm i tns-platform-declarations --save-dev

这里,

tns-platform-declarations 是开发过程中仅在 intelliSense 中所需的开发依赖项。

导入插件

现在,我们已经安装了 nativescript-barcodescanner 插件。让我们使用以下命令将其添加到您的项目中:

const maps = require("nativescript-barcodescanner"); 
maps.requestPermissions();

更新插件

此方法用于更新指定的插件,因此它会卸载以前的插件并安装新版本并修改其依赖项。其定义如下:

tns plugin update <Plugin name version>

删除插件

如果要删除插件(如果不需要),可以使用以下语法:

tns plugin remove <plugin-name>

例如,如果要删除上面安装的 nativescript-google-maps-sdk,请使用以下命令:

tns plugin remove nativescript-barcodescanner

您可能会看到以下响应:

Successfully removed plugin nativescript-barcodescanner

构建插件

它用于构建位于 platforms/android 中的插件的 Android 特定项目文件。让我们使用以下命令构建 nativescript-barcodescanner 插件:

tns plugin build nativescript-barcodescanner

创建插件

NativeScript 插件是简单的 JavaScript 模块。它定义在您的应用程序 src\package.json 文件中。此模块用于为 NativeScript 插件开发创建新项目。其定义如下:

tns plugin create <Plugin Repository Name> [--path <Directory>]
广告