简体   繁体   中英

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.

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

// 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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