Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export more than one variable in ES6?

I'm trying to export more than one variable in ES6:

exports.js

var TestObject = Parse.Object.extend('TestObject') var Post = Parse.Object.extend('Post')  export default TestObject export Post 

main.js:

import TestObject from '../store' import Post from '../store'  var testObject = new TestObject() // use Post in the same way testObject.save(json).then(object => {   console.log('yay! it worked', object) }) 

I understand that there's only one default value so I only used default in the first item.

However, I get this error message:

Module build failed: SyntaxError: /home/alex/node/my-project/src/store/index.js: Unexpected token (9:7)    7 |     8 | export default TestObject >  9 | export Post 

Maybe I'm doing it the wrong way?

like image 610
alexchenco Avatar asked Jan 07 '16 01:01

alexchenco


People also ask

How do I export multiple variables in JavaScript?

Use named exports to export multiple variables in JavaScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

How do I export two variables in React?

Use named exports to export multiple components in React, e.g. export function A() {} and export function B() {} . The exported components can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

Can you export default multiple things?

There are two different types of export, named and default. You can have multiple named exports per module but only one default export.

Can you have more than one module exports?

Answer. We can export more than one object using module.


1 Answers

That is not valid syntax. You can do

export { Post } 

or even just

export var Post = Parse.Object.extend('Post') 

or shorten the whole file to

export default Parse.Object.extend('TestObject') export var Post = Parse.Object.extend('Post') 

Your imports are also incorrect, you'll want to do

import TestObject, { Post } from '../store' 

This is if you really want a single default export and a separate named export. You can also just make two named exports and have no default if you want, e.g.

export var TestObject = Parse.Object.extend('TestObject'); export var Post = Parse.Object.extend('Post'); 

or

var TestObject = Parse.Object.extend('TestObject'); var Post = Parse.Object.extend('Post'); export { TestObject, Post }; 

and import with

import { TestObject, Post } from '../store' 
like image 187
loganfsmyth Avatar answered Sep 24 '22 20:09

loganfsmyth