我有一個JSON來自大約 52,000 行的 API 的示例回應。我已將其保存TestJSONFile.json在我的NextJS專案中。
這不是永久性的,而只是一種臨時措施,因此我可以訪問示例 API 回應,而無需目前連接/實作實際的 API 呼叫。
該專案運行良好,沒有崩潰,但有以下警告訊息:
./src/components/CurrentComponent.js
Should not import the named export 'something' (imported as 'TestJSONFile') from default-exporting module (only default export is available soon)
printWarnings @ hot-dev-client.js?a8a2:88
我能做些什么來擺脫這個警告,只是為了確保以后沒有問題?我對 React 并不完全熟悉,所以我不確定處理這個問題的最佳方法是什么。
我不需要呼叫JSON.parse(),因為一旦我將示例回應手動添加到我的專案中(復制/粘貼整個回應),資料就已經被解釋為 Javascriptobject而不是 JSON 字串。我嘗試將檔案名從更改.json為.js以查看是否可以在沒有警告的情況下以這種方式使用它,但是一旦我更改了擴展名,檔案就會出現錯誤。
TestJSONFile.json好像:
{
"something": [
{
"prop1": "prop1value",
"prop2": "prop2value",
...
},
{
"prop1": "prop1value",
"prop2": "prop2value",
...
},
...
}
在CurrentComponent.js中,我正在匯入JSON值:
import * as SampleResponse from '../tools/TestJSONFile'
然后像這樣訪問資料:
const data = SampleResponse.something // Already a JS object, no need to parse
然后可以像任何其他物件一樣使用它:
for (let item of data) { const prop1 = item.prop1 }
uj5u.com熱心網友回復:
目前您正在匯入 JSON 檔案,如下所示
import * as SampleResponse from '../tools/TestJSONFile'
將其更改為
import SampleResponse from '../tools/TestJSONFile'
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429787.html
