簡體   English   中英

使用導出默認 new ClassName() 時如何使用導入;

[英]How to use imports when using export default new ClassName();

例如:

文件 1:

const env = process.env.VARIABLE;
export const config: any = config[env.toLowerCase()];

文件2:

import { config } from '../file1';
class SomeClass {
  constructor() {
    console.log(config.someProperty);
  }
}
export default new SomeClass();

這似乎不起作用。 config.someProperty 未定義。 有什么理由嗎?

按請求編輯:

import { config } from '../file1';
export class SomeClass {
  private myConfig = config.someProperty;
  constructor() {
    console.log(myConfig);
  }
}

答案是確保在您的環境無法處理循環依賴的情況下不存在循環依賴。 對我來說,我在兩個類之間存在循環依賴(問題中未顯示),這是通過將配置設置移動到其自己的模塊來解決的。

暫無
暫無

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

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