Import and export module ES

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/mynewdays/article/details/102750476

The article describes the ES6 specification may become actual specification of future JS module, this module ES6 specific about how to export and import data

export: Export Data Module

import: import data from other modules

Single Data Export

Creating a.js, type the following code, you have chosen to export some data

export let name='蔡徐坤'
export let age=20

Import variables needed in the b.js

 //这里路径必须为 ./a,而不能是 a,否则会报找不到 a.js
import {name} from './a'
console.log(name)

Note: Export and import of variable names must be the same

Multiple Data Export

May be used simultaneously deriving a plurality of data export has the form of objects

a.js Code

let name='蔡徐坤'
let age=20
export let star={
    name,
    age
}

b.js Code

 //这里路径必须为 ./a,而不能是 a,否则会报找不到 a.js
import {star} from './a'
console.log(star)

Default export

Front everywhere shortcoming: what is called a variable name for the export, import must also name when what

Can be custom variable names may be used if desired export default import module

a.js Code

let name='蔡徐坤'
let age=20
export default {
    name,
    age
}

b.js

 //这里路径必须为 ./a,而不能是 a,否则会报找不到 a.js
import star from './a'
console.log(star)

NOTE: The above code changes each time, the need to use webpack repackaged

Guess you like

Origin blog.csdn.net/mynewdays/article/details/102750476