“ import * from”和“ import * as”是“ from””有什么区别?

时间:2019-01-12 11:33:11

标签: javascript babel

我正在将JavaScript与webpack4和babel7一起使用。我对使用import * from ''import * as '' from ''感到困惑。

以下代码:

import tbc from `tbc`

tbc实例将具有default属性,我必须使用tbc.default.xxx。根据我的理解,它应该等于import {default as lib } from 'lib';。但是为什么它具有default属性?

但是下面的代码将允许我使用tbc中的所有属性,例如tbc.xxx

import * as tbc from `tbc`

我想知道何时应该使用import * from

1 个答案:

答案 0 :(得分:1)

文件-nums.js

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

文件-index.js

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

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

文件-index.js 您可以一次将所有值作为对象导入*作为obj导入,例如:

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