我正在使用 ThreeJS 制作一個專案并嘗試匯入一個向渲染器添加 ASCII 效果的 js 函式,但無論我怎么做,我似乎都會出錯。我嘗試了幾種語法并更改了 TS 配置,但沒有運氣。
編輯:剛剛意識到我應該澄清一下,我直接從 github repo 下載了 AsciiEffect 函式作為 js 檔案,所以我試圖將一個函式從我下載的檔案“AsciiEffect.js”匯入到我的主 TS 檔案“main.ts ”。我不是從 ThreeJS 依賴項匯入的,因為它顯然不是包本身的一部分。
TS 組態檔
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "commonJS",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
"isolatedModules": true,
"esModuleInterop": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"allowJs": true
},
"include": ["src"]
}
目前我正在嘗試像這樣匯入
import './style.css'
import * as THREE from 'three';
import {AsciiEffect} from './effects/AsciiEffect.js'
并像這樣從 AsciiEffect.js 匯出
export {AsciiEffect}
這在瀏覽器中給了我這個錯誤 Uncaught SyntaxError: Export 'AsciiEffect' is not defined in module
我嘗試過使用其他語法但沒有運氣,這是我第一個嘗試使用 typescript 的專案,我實際上希望效果在 ThreeJS 依賴項中。我嘗試重做我迄今為止在 JS 中所做的事情,但得到了同樣的錯誤。如果有幫助的話,該專案是使用 Vite 創建的。它只是一個沒有框架的默認 TS 專案。
uj5u.com熱心網友回復:
你有一個錯誤的匯出,像這樣匯出函式:
export AsciiEffect
然后 TS 編譯器會抱怨它不能識別那個模塊,因為它是一個.js模塊而不是 TS 模塊,所以你需要添加自定義模塊宣告:
- 如果您的專案的根目錄中還沒有
*.d.ts檔案,請創建一個名為declarations.d.ts. - 在其中添加以下行:
declare module "path/to/the/module";
這樣你就告訴 TS 這是一個合法的匯入模塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/468676.html
標籤:javascript 打字稿
