node.js - 导入时强制转换类的名称以防止“不是构造函数”错误
问题描述
导入时可以转换导出类的名称吗?
出口:
class ResponseObj {}
const TYPES = {}
module.exports = { ResponseObj, TYPES };
并导入:
const { response, responseTypes } = require(cfg.libs + 'message_response');
现在我在运行时收到错误“响应不是构造函数”:
var n = new response()
我怎样才能只为一个文件转换它并为其他文件保留 ({ ResponseObj, TYPES })?
解决方案
const { ResponseObj:response , TYPES:responseTypes } = require(cfg.libs + 'message_response');
就是你要找的。
更普遍,
const { exportedKey: newKeyName } = require('something);
// use newKeyName this file
推荐阅读
- python - 一个关于在 Python 中选择子数组的问题
- android - 带有 MotionLayout 的 TextView 可绘制旋转
- javascript - 如何在函数上创建数组显示?
- javascript - PHP函数:无法在Javascript Onclick事件中传递字符串值
- python - 如何在 Linux 中通过 Selenium ChromeDriver 和 Python 打开 Chromium 浏览器
- python - 将 pywin32 与 zmq 一起使用时出现奇怪的问题
- ios - 如何在物镜中的 UIImage 上添加球形滤镜效果(鱼眼) - C 或实时相机
- php - 使用ffmpeg根据视频分辨率调整水印
- visual-studio-code - tsserver 日志在哪里?
- delphi - FMX Delphi ComboBox 排序不存在