ES6 exposure module and the module is introduced

ES6 modules and exposure allows us to realize the introduction of modular programming, listed below ES6 several modules exposed manner distinguish introduced.

1, ES6 exposure method of a total of three modules

  • Multi-line exposure

Module 1:module1.js

//多行暴露
export function foo() {
    console,console.log('foo() moudle1');
}

export function bar() {
    console.log('bar() moudle1')
}
  • Uniform exposure

Module 2: module2.js

function fun1() {
    console.log('fun1() module2')
}

function fun2() {
    console.log('fun2() module2')
}
// 统一暴露
export {foo,bar}

Outwardly exposed above two ways must be deconstructed object when introduced into the main file assignment reference (embodiment not use a variable to the received reflection)
main module:main.js

import {foo,bar} from '.js/src/module1.js'
import {fun1,fun2} from './js/src/module2.js'
  • Default exposure
export default {
    foo() {
        console.log('默认暴露方式')
    },
    bar() {
        console.log('默认暴露')
    }
}

The default exposure allows only one way: export default {}and define variables used may be received when introduced into the main module way!

// 引入模块3
import module3 from '.js/src/module3.js'

// 使用模块
module3.foo()
module3.bar()

Guess you like

Origin www.cnblogs.com/yuanchao-blog/p/10985884.html