我的“/constants”檔案夾中有一個檔案“test.js”,內容如下:
const test = "test!"
export default test
我在“/pages”檔案夾中的頁面應該從“test.js”中讀取字串并列印出來
import { test } from "../constants/test"
export default function Home() {
console.log("imported string: " test)
}
如果我在瀏覽器中運行它,我會得到以下輸出:
“匯入的字串:未定義”
為什么不從檔案中讀取字串?路徑是正確的。VSCode 自動完成功能甚至可以在鍵入時找到檔案。
uj5u.com熱心網友回復:
您正在將常量測驗匯出為默認匯出,因此您的匯入陳述句應該從
import { test } from "../constants/test"
至
import test from "../constants/test"
注意:{} 匯入用于非默認匯出
uj5u.com熱心網友回復:
如果你想使用import { test } 那么你應該使用export test而不是使用default
因為現在你告訴javascript找到特定的匯出,而不是默認的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/535709.html
上一篇:在物件陣列中按鍵獲取值
下一篇:如何根據日期計算天數
