What is the difference between these ES6 import methods?

What is the difference between these import methods?

Method 1:

import {sum, pi} from "lib/math";

Method 2:

import exp, {pi, e} from "lib/mathplusplus";

The es2015 docs showed these two examples, and I can't figure out the purpose of the curly braces. It seems that all the things listed after import will still be attached to the object window.

Documents for reference: https://babeljs.io/docs/learn-es2015/

+4
source share
3 answers
Modules

can export a few things. Modules can also have one export by default.

import exp from "somelib";

This sets the somelibdefault export for the variable exp.

import {a, b} from "somelib";

a b a b.

import exp, {a, b} from "somelib";

exp, a b.

import * as somelib from "somelib";

somelib somelib, , somelib.a, somelib.b ..

: http://www.2ality.com/2014/09/es6-modules-final.html

+7

exp - default, , exp. pi e , .

:

export default function(x) {
  return x + x;
}

, , :

import double from 'mymodule';
double(2); // 4
+1

Modules can be in exporttwo different ways. They can use defaultor just perform standardexport

export default function exp(value, power) {}
export const pi = 3.14159

When you are importout of a module, you need to use curly braces to capture the default export. If you want to export by default, you do not need parentheses.

import exp, {pi} from "lib/mathplusplus";
+1
source

Source: https://habr.com/ru/post/1619075/


All Articles