首页 > 解决方案 > 如何在打字稿中导入 json 文件?

问题描述

我有一个 json 文件说 data.json,

[
   {
       "page" : "First" ,
       "subpage" : "FirstA",
       "title" : [
           {
               "one" : "Heading 1",
               "two" : "Heading 2"
           }
       ]
   },...
]

如何在我的“tsx”文件中导入它?

import data './location';

似乎抛出错误:

{
    "message": "Cannot find module 'location' or its corresponding type declarations."
}

在我添加的 tsconfig.json 中,

    "resolveJsonModule": true,
    "esModuleInterop": true,...

连同其他配置..

关于我可能遗漏的任何想法?

标签: reactjstypescript

解决方案


推荐阅读