简体   繁体   English

将对象中的所有属性导出为非默认值

[英]export all properties from object as non default

I've been wanting to do this, but it doesn't seem to be working我一直想这样做,但它似乎不起作用

const settings = {
  PENDING_ACTION_TIME: 100000
}

export default settings
export {...settings}

This way I can do这样我可以做到

import {PENDING_ACTION_TIME} from '../settings'

or或者

import settings from '../settings'

I know I could export each properties one by one by that seems like stupid code repetition if you have something like 20 properties.我知道我可以一个一个地导出每个属性,如果你有 20 个属性,这似乎是愚蠢的代码重复。

Don't start with an object in the first place.首先不要从对象开始。 Use named exports instead:改用命名导出:

const PENDING_ACTION_TIME = 100000;

This way you can do这样你就可以做到

import {PENDING_ACTION_TIME} from '../settings';

or a namespace import命名空间导入

import * as settings from '../settings';

You could also use the namespace object in a default export, but you really shouldn't.您也可以在默认导出中使用命名空间对象,但您确实不应该这样做。

Maybe using the CommonJS method of export也许使用 CommonJS 的导出方法

// settings.js

module.exports = {
  PENDING_ACTION_TIME: 100000
}

But you'll only be able to use the following syntax但是您将只能使用以下语法

import { PENDING_ACTION_TIME } from '../settings';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM