admin管理员组

文章数量:1293917

I have a a list of constants which I am exporting

export const ABC="abc"
export const DEF="def"
...

Is there a way I can export them in a single line .

I am aware of something like this

const ABC="abc"
const DEF="def"
...

export {ABC,DEF...}

but is there a way something like

export {
//define constants here
}

I have a a list of constants which I am exporting

export const ABC="abc"
export const DEF="def"
...

Is there a way I can export them in a single line .

I am aware of something like this

const ABC="abc"
const DEF="def"
...

export {ABC,DEF...}

but is there a way something like

export {
//define constants here
}
Share asked Jul 8, 2021 at 6:14 Akshay SharmaAkshay Sharma 671 silver badge5 bronze badges 5
  • maybe module.exports = {ABC: 'abc', DEF: 'def'}, i never tried though – boxdox Commented Jul 8, 2021 at 6:18
  • 1 @boxdox - That's CJS. The above is ESM. – T.J. Crowder Commented Jul 8, 2021 at 6:18
  • What's the thing about export { /* define constants here */ } that you like better than export const ABC = "abc"; export const DEF = "def";? To me those are basically the same. Is it the repeated export? – T.J. Crowder Commented Jul 8, 2021 at 6:19
  • @T.J.Crowder dang it i keep messing it up. yeah, i guess op wants to define constant inside the exports itself – boxdox Commented Jul 8, 2021 at 6:20
  • Yes it would be great if I can export all at once instead of having a repeat export – Akshay Sharma Commented Jul 8, 2021 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 8

You can declare multiple constants in the same line. Clever use of spacing gives you this:

// a.js
export const
  ABC = "abc",
  DEF = "def",
  GHI = "ghi";

//b.js

import { ABC, DEF, HGI } from './a.js';

Note the ma between lines and the semicolon in the last one.

Prettier would like to have a chat with you though.

No, you have to do one of the two things you listed (export them as you declare them, or use an export declaration gathering them together). (If it helps, it doesn't matter where the export { ... } is in relation to the constants; it can be above them if you prefer [yes, really

本文标签: javascriptIs there a way I can export multiple constantsStack Overflow