我试图在我的React.js项目中将JSON文件作为类型对象导入。我之所以不直接在源代码中导入json文件,是因为我后来想让webpack根据环境设置决定应导入哪个配置文件。
JSON文件:
{
"baseUrl": "crm.dev",
"endpoints": {
"getOptions": "getoptionsurl",
"addOptionToApplication": "",
"deleteOptionFromApplication": ""
}
}
我在我的webpack外部配置中定义了一个JSON模块,如下所示:
...
externals: {
crmConfig: path.join(APP_PATH, "/config/crmConfig.prod.json")
}
我在我的types.d.ts中将模块定义为:
declare module "crmConfig" {
export interface CrmConfig {
baseUrl: string
endpoints: {
getOptions: string
addOptionToApplication: string
deleteOptionFromApplication: string
}
}
const crmConfig: CrmConfig
export default crmConfig
}
当我尝试导入模块时,像这样记录对象:
import crmConfig from "crmConfig";
console.log(crmConfig)
我收到以下错误:
Uncaught SyntaxError: Unexpected token :
at Object.crmConfig (bundle.js:4709)
at __webpack_require__ (bundle.js:20)
at eval (index.ts:5)
at Module../src/stores/index.ts (bundle.js:4687)
at __webpack_require__ (bundle.js:20)
at eval (App.tsx:7)
at Module../src/components/App/App.tsx (bundle.js:4616)
at __webpack_require__ (bundle.js:20)
at eval (index.tsx:6)
at Module../src/index.tsx (bundle.js:4663)
JSON文件似乎是有效的,因此我无法确切弄清楚我做错了什么。有人有建议吗?
预先感谢