我有一個用純 javascript(index.html、script.js)撰寫的基本應用程式,我將它放入一個CapacitorJs專案中,讓我創建 ios/android 應用程式。即使在我的手機上也能正常作業。
但現在我正在嘗試使用電容器社區插件(@capacitor-community/contacts),但我需要像這樣將它匯入我的 script.js
import { Contact, Contacts, NewContact } from '@capacitor-community/contacts';
由于我沒有使用打字稿,因此顯然會引發錯誤。
我怎樣才能使它作業?
我試圖通過
<script type="module">我的 index.html 上的 a 匯入它并將其分配給 js 全域window變數以在我的 script.js 中訪問它,但它不起作用。我還嘗試將我的 scripts.js 轉換為 typescript 并使用以下配置對其進行編譯:
{
"target":"es5",
"module":"es2015",
"moduleResolution:"node"
}
為了使其作業,必須將匯入字串更改為:
import { Contact, Contacts, NewContact } from '../../nodes_modules/@capacitor-community/contacts';
但是后來我在控制臺上收到一個錯誤說TypeError: 'text/html' is not a valid Javascript MIME type
- 我也嘗試過以下配置
"target":"commonjs",
但是在瀏覽器中無法識別“定義”時出錯。
- 我也知道
"bundledWebRuntime": true,基本上將電容器核心檔案編譯成您在 index.html 中匯入的 .js。但它不包括社區包,所以它不是一個解決方案。
uj5u.com熱心網友回復:
它與打字稿無關。您的專案必須宣告為使用 ES 模塊,才能使用import. 我對您的確切設定有點不清楚,但是如果您有package.json,請嘗試將其添加到其中:
{
...
type: "module",
}
如果您只有一個基本設定,即在腳本標簽中帶有 js 檔案的 html 頁面,您應該能夠通過將 js 檔案宣告為這樣的模塊來使其作業:
<script type="module" src="main.js"></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424267.html
標籤:javascript 科尔多瓦 电容器
