我正在将现有的JavaScript项目转换为类型脚本.我不知道如何导出对象来获取此javscript输出
const path = require('path'), rootPath = path.normalize(__dirname + '/..'), env = process.env.NODE_ENV || 'development'; let config = { development: { amqpUrl: "amqp://localhost:15672", root: rootPath }, test: { amqpUrl: "amqp://localhost:5672", root: rootPath }, production: { amqpUrl: "amqp://localhost:5672", root: rootPath } }; module.exports = config[env];
我写了如下的打字稿,但不清楚导出
import path = require("path") const rootPath = path.normalize(__dirname + '/..') const env = process.env.NODE_ENV || 'development' let config = { development: { amqpUrl: "amqp://localhost:15672", root: rootPath }, test: { amqpUrl: "amqp://localhost:5672", root: rootPath }, production: { amqpUrl: "amqp://localhost:5672", root: rootPath } }; /* this is the line i'm having problem how can i export config object*/ // export config[env];
当我导出我如何导出我试过的对象,export default config[env]
但它产生一些从预期输出的顺序
在ES6中,您可以使用导出功能导出名称,或者默认情况下可以导出任何内容.该require
格式是这样的:
let config = require('config')
它需要配置文件的默认导出.在你的情况下,你应该这样做:
export default config[env]
如果您想使用导出,您可以执行以下操作:
let Environment = config[env]; export {Environment}
区别在于:
import EnvirmentNameWhatever from "./config"
至
import {Environment} from "./config"
注意 - 默认导出时,您可以使用任何您喜欢的名称,而在导出时,您必须使用导出的名称.
使用export
要导出的声明上的关键字应该完成这项工作,如下所示:
import path = require("path") const rootPath = path.normalize(__dirname + '/..') export const env = process.env.NODE_ENV || 'development' export let config = { development: { amqpUrl: "amqp://localhost:15672", root: rootPath }, test: { amqpUrl: "amqp://localhost:5672", root: rootPath }, production: { amqpUrl: "amqp://localhost:5672", root: rootPath } };