I have the following imports:
import { default as service } from "../service";
VS
import * as service from "../service";
My service is exported like so
module.exports = {
init(store) {
_store = store;
},
beginPayment() {
}
};
I would expect that only the second import would work since there is no export default, however both seem to work.
What is the difference between these? Is one preferred over the other?
If this is a duplicate I apologize, I didn't find anything specific to my example on SO or Google.
I have the following imports:
import { default as service } from "../service";
VS
import * as service from "../service";
My service is exported like so
module.exports = {
init(store) {
_store = store;
},
beginPayment() {
}
};
I would expect that only the second import would work since there is no export default, however both seem to work.
What is the difference between these? Is one preferred over the other?
If this is a duplicate I apologize, I didn't find anything specific to my example on SO or Google.
Share Improve this question asked Nov 20, 2018 at 15:58 Brandon McAleesBrandon McAlees 7753 gold badges12 silver badges30 bronze badges 3- Possible duplicate of When should I use curly braces for ES6 import? – Pete Commented Nov 20, 2018 at 16:16
- I didn't see the import of: { default as 'xxx' } explained in that question. Still unsure of exactly what the differences are here. – Brandon McAlees Commented Nov 20, 2018 at 16:21
- It's the bottom one in the accepted answer, starts with We can also assign them all different names when importing: – Pete Commented Nov 21, 2018 at 10:03
1 Answer
Reset to default 14If you are importing the default, there has to be a default.
In general, the community appears wary of default exports at the moment as they seem to be less discoverable (I have no specific citation, but I've watched the conversation!)
If you are working in a team, whatever they say is the correct answer, of course!
So without a default, you need to use:
import * as service from "../service";
Or choose a specific thing:
import { specificNamedThing } from "../service";