hidar
hidar

Reputation: 5939

How to export multiple object from files

I have a list off foods and drinks that I want to export from a file like this:

// example.js
var foods = ['', ''];
var drinks = ['', ''];

I would like to export those, so I can import them as:

import {foods, drinks} from './example.js'
console.log(foods); // ['', '']

My question is, how should I export them in the first example to get them in the second example

Usually I export one object from a file and that works, but multiple I am not sure

Upvotes: 10

Views: 11681

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27192

You can export multiple objects like this in ES6 :

// example.js
var foods = ['', ''];
var drinks = ['', ''];

export {
    foods,
    drinks
}

Then, when importing you do it like this :

import { foods, drinks } from './example.js';

For more info, check this import and export.

Upvotes: 11

Meghan
Meghan

Reputation: 1261

// example.js
export const foods = [...
export const drinks = [...

Use the export keyword, and by not including the default keyword, your import code should work just as you submitted it.

Upvotes: 13

Related Questions