There are two main ways in which you can import code from another file (module):
- Import only the exports you are interested on (using curly braces)
- Import all the exports into an object (using
import * as ...
).
However, keep in mind that the option you choose can impact the way you name your exports.
If you are going to import only the exports you are interested on you may want to append more information to the name of each export. For example:
// users.service.js
export function listUsers() {}
export function createUser() {}
// controller.js
import { listUsers, createUser } from './users.service'
On the other hand, if you are going to import all the exports into an object you can shorten the names of the exports
// users.service.js
export function list() {}
export function create() {}
// service.js
import * as userService from './users'
It seems to me that userService.list()
is a bit more clearer and simpler.
Top comments (0)