0

I am using javascript with webpack4 and babel7. I am confused about using import * from '' and import * as '' from ''.

for below code:

import tbc from `tbc`

the tbc instance will have a default property and I have to use tbc.default.xxx. Based on my understanding, it should be equal to import {default as lib } from 'lib';. But why does it have a default property?

But below code will allow me to use all properties from tbc, such as tbc.xxx.

import * as tbc from `tbc`

I wonder when I should use import * from.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • 1
    Possible duplicate of [Difference between import X and import \* as X in node.js (ES6 / Babel)?](https://stackoverflow.com/questions/31386631/difference-between-import-x-and-import-as-x-in-node-js-es6-babel) and [When to use “import * as Foo” versus “import Foo”?](https://stackoverflow.com/questions/32236163) – adiga Jan 12 '19 at 11:42
  • There is no `import * from '…'` syntax, only `import * as … from '…'` or `import … from '…'` (the latter of which is equivalent to `import { default as … } from '…'`). – Bergi Jan 12 '19 at 12:32

1 Answers1

2

file - nums.js

export let one = 1;
export let two = 2;

file - index.js

import {one, two} from "./nums";

alert( `${one} and ${two}` ); // 1 and 2

file - index.js You can import all values at once as an object import * as obj, ex:

import * as numbers from "./nums";
alert( `${numbers.one} and ${numbers.two}` ); // 1 and 2