【发布时间】:2019-01-18 07:44:31
【问题描述】:
我有一个文件config.ts,它只是导出一个配置对象:
const config = {
baseURL: <string> 'http://example.com',
};
export default config;
我有另一个名为 methods.ts 的文件,它导入配置对象并导出使用此配置对象的函数。
import config from './config';
export function someMethod() {
let url = config.baseURL;
...
}
我在 express 路由器内部调用这个someMethod:
import { someMethod } from '../methods';
router.get('/something', function(req, res, next) {
let x = someMethod();
...
});
当这个someMethod 被调用时,config 变量是undefined。 someMethod 似乎在稍后调用时无法从同一文件中看到导入的数据。这样做的正确方法是什么?
【问题讨论】:
标签: node.js express typescript