Module usage in es6
- Keyword
import
import module - Keyword
export
import module
export
create a new filemj.js
export function plus(a, b) {
return a + b
}
import
Import needs to be placed in the script tag of type module
import * as m from './mj.js';
alert(m.plus(1, 2));
Use import() to import the module, the script where import() is located does not need to set the type to module. The
import() function returns a Promise object
import('./mj.js').then(r => {
alert(r.plus('hello', 'world'))
})
Complete code
module
Code
export function plus(a, b) {
return a + b
}
html
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>es6 模块</title>
</head>
<body>
<script type="module">
import * as m from './mj.js';
alert(m.plus(1, 2));
</script>
</body>
</html>