Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a class after definition in ES6

// test.js
class Test

export Test

// index.js
import {Test} from './test'

This results in a syntax error with Unexpected token. What is the correct way to export a predefined class?


EDIT: It is required that the class definition is separate from the export.

like image 472
vaughan Avatar asked Jun 18 '15 16:06

vaughan


People also ask

Can I export a class in JavaScript?

Use named exports to export a class in JavaScript, e.g. export class Employee {} . The exported class can be imported by using a named import as import {Employee} from './another-file. js' . You can use as many named exports as necessary in a file.

What is the correct way of exporting a component in ES6?

ES6 provides two ways to export a module from a file: named export and default export. With named exports, one can have multiple named exports per file. Then import the specific exports they want surrounded in braces. The name of imported module has to be the same as the name of the exported module.

How do I export a class function?

Use __declspec(dllexport) to export the function or method. You can export an entire C++ class by placing the __declspec(dllexport) before the class name, or you can export a single method by placing __declspec(dllexport) before the method name. Make class methods static and public.


1 Answers

The correct way to do it is to use export {Test}.

like image 111
vaughan Avatar answered Oct 01 '22 01:10

vaughan