Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 export object properties separately

Say, I have an object like {foo: 5, bar: 10} and I wanna export foo and bar from it separately, then when I do import {foo} from './path/to/file';

I could get foo equal to 5, as if I did export const foo = 5; export const bar = 10;

How can I do it?

like image 270
Tristan Tzara Avatar asked Jun 06 '16 11:06

Tristan Tzara


1 Answers

Exported values need their own top-level variable name. The easiest option might be something like this:

const obj = {foo: 5, bar: 10};

export const {foo, bar} = obj;

Really though, if the object is already declared in the file, you may be better off declaring the exports with the values directly.

like image 116
loganfsmyth Avatar answered Sep 17 '22 15:09

loganfsmyth