簡體   English   中英

如何使用VueJS / Typescript導入JSON文件?

[英]How do you import a JSON file with VueJS / Typescript?

我剛剛創建了一個干凈的VueJS新項目,但無法加載JSON文件才能正常工作。

復制/發行

為了便於復制,我創建了一個帶有問題的Github存儲庫: https : //github.com/devedse/VueJSImportJsonFile

在Home.vue頁面中,我試圖讓Json加載工作。 https://github.com/devedse/VueJSImportJsonFile/blob/master/src/views/Home.vue

當您在VSCode中打開此解決方案時,以下行顯示錯誤:

import theJson from '@/assets/hi.json';

VSCode中的錯誤 Can't find module '@/assets/hi.json'

運行NPM服務時,會彈出以下錯誤: 在此處輸入圖片說明

我已經嘗試過的

我已經搜索了所有stackoverflow並嘗試了以下文章中的所有內容:

在TypeScript中導入json文件

導入json文件時出現Typescript編譯器錯誤

https://github.com/chybie/ts-json

編輯1:

好的,我現在設法通過將其添加到tsconfig.json來使其在運行npm run serve時起作用:

{
  "compilerOptions": {
    "resolveJsonModule": true
  }
}

但是,VSCode中的錯誤似乎仍然存在。 有沒有辦法解決這個問題?

只需添加resolveJsonModule": truetsconfig.jsoncompilerOptions

diff --git a/tsconfig.json b/tsconfig.json
index 499f5e2..a05dab1 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -6,6 +6,7 @@
     "jsx": "preserve",
     "importHelpers": true,
     "moduleResolution": "node",
+    "resolveJsonModule": true,
     "esModuleInterop": true,
     "allowSyntheticDefaultImports": true,
     "sourceMap": true,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM