簡體   English   中英

從 typescript 中的索引文件導出 JSON 文件

[英]export JSON file from index file in typescript

在我的 typescript 項目中,我能夠在文件夾的 index.ts 中重新導出我的默認導出。

例如:

export { default as BaseError } from "./errorResponse";

但我無法像這樣導出 JSON 文件。

export * as configFiles from "./config";

如何捆綁和導出 JSON 文件,以便我可以在 tsconfig 的路徑中使用它

{
  "compilerOptions": {
    "paths": {
      "@errorClass": ["src/helpers/errorClasses"],
      "@config": ["src/config"],
      "@routes": ["src/routes"]
    },
}

tsconfig.json文件中添加以下選項:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

更新tsconfig文件后導出 configFile,如下所示:

export { default as configFiles } from "./config.json";

現在要在您的代碼中configFiles配置文件,請使用以下代碼:

import { configFiles } from "./file-path-to-configFiles-variable";

console.log(configFiles);

示例tsconfig.json

"compilerOptions": {
        "baseUrl": "src",
        ...
        "paths": {
            "@config":["src/config/*"],
         ...
        },`
import { xConfig } from '@config/index';

暫無
暫無

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

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