NativeScript - 外掛



npm 包用於新增原生功能。使用此包,我們可以安裝、搜尋或刪除任何外掛。本節詳細介紹了外掛。

命令

add − 用於安裝外掛。

update − 更新指定的外掛並修改其依賴項。

remove − 刪除外掛。

build − 用於為 iOS 或 Android 專案構建外掛。

create − 為您的專案建立外掛。

新增外掛

以下語法用於新增新外掛:

tns plugin add <plugin-name>

例如,如果要新增 nativescript-barcodescanner,可以使用以下程式碼:

tns plugin add nativescript-barcodescanner

您可能會看到以下響應:

+ nativescript-barcodescanner@3.4.1 
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>]
廣告

© . All rights reserved.