簡體   English   中英

如何將一個配置文件導入到 typescript 中的多個文件中

[英]How to import a config file into multiple files in typescript

我有一個 nodeJs 應用程序,它使用 sysconfig.ts 文件來存儲一些系統信息等。在 javascript 中,我可以在任何模塊中需要這個文件,比如通過

const config = require('../../config/sysconfig')

這適用於第一個文件,但如果我嘗試在后續文件中執行此操作,則會出現以下錯誤

TS2451:無法重新聲明塊范圍變量“config”。

好的,簡單的解決方法是在每個文件中調用不同的配置,但我認為這不是解決這個問題的最佳解決方案。 這是我的配置文件的樣子

module.exports = {
    'host': '127.0.0.1',
    'port': '8080',
    'portSecure': '443',
    'tokenlife': '5min'}

你可以使用這樣的東西:

const config =  {
    host:"127.0.0.1",
  port: "8080",
  portSecure: "443",
  tokenlife: "5min"
  }

export default config;

// and import like this
import config from "./yourconfigpath/configFile"

暫無
暫無

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

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