JavaScript - Default export, Named export
JavaScript에서 모듈을 다른 파일로부터 가져올 때, 내보내기(export)와 가져오기(import)의 방식에는 두 가지 주요한 유형이 있습니다: **기본 내보내기 (Default export)**와 **명명된 내보내기 (Named export)**입니다.
1. **기본 내보내기 (Default export)**
- 한 파일에 하나의 기본 내보내기만 가능합니다.
- 가져올 때 중괄호 `{ }`를 사용하지 않습니다.
```javascript
// routes.js
const routes = [...];
export default routes;
// 다른 파일에서 가져올 때
import routes from './routes.js';
```
2. **명명된 내보내기 (Named export)**
- 한 파일에 여러 명명된 내보내기가 가능합니다.
- 가져올 때 중괄호 `{ }`를 사용합니다.
```javascript
// routes.js
export const routes = [...];
export const anotherVariable = ...;
// 다른 파일에서 가져올 때
import { routes, anotherVariable } from './routes.js';
```
여러분의 코드에서 `routes.js` 파일에서 `routes`를 **기본 내보내기**로 내보냈습니다. 그래서 다른 파일에서 가져올 때는 `import routes from './routes.js';`와 같이 중괄호 없이 가져와야 합니다.
즉, `import { routes } from "./routes.js";`와 같은 방식으로 가져오려면, `routes.js`에서 `export const routes = ...;`와 같이 **명명된 내보내기**로 내보내야 합니다.
댓글
댓글 쓰기