source

ES6 모듈에서 여러 클래스 내보내기

bestscript 2022. 11. 24. 23:41

ES6 모듈에서 여러 클래스 내보내기

여러 ES6 클래스를 내보내는 모듈을 만들려고 합니다.예를 들어 다음과 같은 디렉토리 구조를 가지고 있다고 합시다.

my/
└── module/
    ├── Foo.js
    ├── Bar.js
    └── index.js

Foo.js그리고.Bar.js각각 기본 ES6 클래스를 내보냅니다.

// Foo.js
export default class Foo {
  // class definition
}

// Bar.js
export default class Bar {
  // class definition
}

나는 현재 나의index.js다음과 같이 설정합니다.

import Foo from './Foo';
import Bar from './Bar';

export default {
  Foo,
  Bar,
}

하지만 저는 수입을 할 수 없습니다.이 작업을 수행할 수 있으면 좋겠는데 클래스를 찾을 수 없습니다.

import {Foo, Bar} from 'my/module';

ES6 모듈에서 여러 클래스를 내보내는 올바른 방법은 무엇입니까?

코드로 시험해 보세요.

import Foo from './Foo';
import Bar from './Bar';

// without default
export {
  Foo,
  Bar,
}

참고로 다음과 같이 할 수도 있습니다.

// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
export { default } from './Baz'

// and import somewhere..
import Baz, { Foo, Bar } from './bundle'

사용.export

export const MyFunction = () => {}
export const MyFunction2 = () => {}

const Var = 1;
const Var2 = 2;

export {
   Var,
   Var2,
}


// Then import it this way
import {
  MyFunction,
  MyFunction2,
  Var,
  Var2,
} from './foo-bar-baz';

와의 차이점export default는, 무엇인가를 export 해, Import처의 이름을 적용할 수 있는 것입니다.

// export default
export default class UserClass {
  constructor() {}
};

// import it
import User from './user'

이것이 도움이 되기를 바랍니다.

// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}

// if using `eslint` (airbnb) then you will see warning, so do this:
const MyFunction1 = () => {}
const MyFunction2 = () => {}
const MyFunction3 = () => {}

export {MyFunction1, MyFunction2, MyFunction3};

// Import
import * as myFns from "./my-functions";

myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();


// OR Import it as Destructured
import { MyFunction1, MyFunction2, MyFunction3 } from "./my-functions";

// AND you can use it like below with brackets (Parentheses) if it's a function 
// AND without brackets if it's not function (eg. variables, Objects or Arrays)  
MyFunction1();
MyFunction2();

@webdeb의 대답이 통하지 않아서unexpected tokenES6와 Babel을 컴파일할 때 이름이 지정된 기본 내보내기를 수행할 때 오류가 발생합니다.

하지만, 이것은 나에게 효과가 있었다.

// Foo.js
export default Foo
...

// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
...

// and import somewhere..
import { Foo, Bar } from './bundle'
// export in index.js
export { default as Foo } from './Foo';
export { default as Bar } from './Bar';

// then import both
import { Foo, Bar } from 'my/module';

같은 장소에 여러 개 있는 경우js파일, 확장Component부터@wordpress/element다음과 같이 할 수 있습니다.

// classes.js
import { Component } from '@wordpress/element';

const Class1 = class extends Component {
}

const Class2 = class extends Component {
}

export { Class1, Class2 }

다른 곳으로 Import합니다.js파일:

import { Class1, Class2 } from './classes';

할수있습니다.내보내기 {className, className 등}

클래스의 인스턴스를 내보내는 경우 다음 구문을 사용할 수 있습니다.

// export index.js
const Foo = require('./my/module/foo');
const Bar = require('./my/module/bar');

module.exports = {
    Foo : new Foo(),
    Bar : new Bar()
};

// import and run method
const {Foo,Bar} = require('module_name');
Foo.test();

언급URL : https://stackoverflow.com/questions/38340500/export-multiple-classes-in-es6-modules