最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between import * as & import { default as } - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 14

If 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";
发布评论

评论列表(0)

  1. 暂无评论