node.js - 在“TypeScript”中使用 node-config 的 deferConfig 时“无法读取属性”
问题描述
我在 TypeScript 中使用节点配置。
我有如下代码
config/default.ts
::
import {deferConfig} from 'config/defer';
export default {
service: {
url: 'http://localhost',
errorUrl: deferConfig(function() {
console.info('config before defer:', this);
return `${this['service']['url']}/error.html`;
}),
},
};
src/demo.ts
:
import config from 'config';
console.info('resulting config: ', config)
当我使用ts-node
.
$ npx ts-node ./src/demo.ts
config before defer: { service: { url: 'http://localhost', errorUrl: [Getter] } }
resulting config: Config {
service: { url: 'http://localhost', errorUrl: 'http://localhost/error.html' }
}
但是,如果我运行用 编译的代码tsc
,我会得到
$ NODE_CONFIG_DIR=./build/config ./build/src/demo.js
config before defer: {
default: { service: { url: 'http://localhost', errorUrl: [Getter] } }
}
.../build/config/default.js:9
return `${this['service']['url']}/error.html`;
^
TypeError: Cannot read property 'url' of undefined
的值this
有一个额外的水平default
。
为什么 ts 和 js 代码的行为不一致?如果我更改export default
为export =
,它适用于 ts 和 js 代码。
但是,文件要求node-config
我使用export default
.
我应该export =
改用吗?
解决方案
推荐阅读
- node.js - 快速验证器在 multer 函数后不执行
- php - 使用 Laravel 为 Twilio SMS 验证添加本地化
- json - Python Dict 解析问题
- reactjs - 使用关键路线的“快照”(静态渲染)加速大型 React 项目的最佳方法
- javascript - 三个没有定义;我该如何解决?
- python - 如何以可读的方式级联尝试导入
- php - 使用数组填充表格
- python - 向 GeoJsonToolTip 添加另一个值时出现 TypeError
- c# - 如何将字符串数组传递给属性构造函数?
- java - 递归类型序列化期间的 Jackson RuntimePermission 异常