命名导出: 以名称区分的导出称为命名导出。我们可以使用命名导出导出多个变量和函数。
默认导出: 使用默认导出最多可以导出一个值。
//Named export in class class Nokia{ //properties //methods } export {Nokia}; //Named export //Named export in functions function show(){ } export {show}; //Named export in Variables const a = 10; export {a};
class Nokia{ //properties //methods } function show(){ } const a = 10; export {Nokia, show};
import {Nokia, show} from './Mobile.js';
import * as device from './Mobile.js'; // Here, the device is an alias, and Mobile.js is the module name.
device.Nokia //if we have a class Nokia device.show // if we have a function show device.a // if we have a variable a
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script type = "module" src = "App.js"></script> <title>Document</title> </head> <body> <h1>ES6 Modules import and export</h1> </body> </html>
class Display{ show(){ console.log("Hello World :)"); } } function javaT(){ console.log("Welcome to lidihuo"); } export {Display,javaT};
import {Display,javaT} from "./Mobile.js"; const d = new Display(); d.show(); javaT();
//default export in class class Nokia{ //properties //methods } export default Nokia; //default export //Deafult export in functions function show(){ } export default show; //default export in Variables const a = 10; export default a;
class Nokia{ //properties //methods } export default Nokia;
import Nokia from './Mobile.js';
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script type = "module" src = "App.js"></script> <title>Document</title> </head> <body> <h1>ES6 Modules import and export</h1> <h2>default import and export</h2> </body> </html>
class Display{ show(){ console.log("Hello World :)"); console.log("default Import and Export Example"); } } export default Display;
import Display from "./Mobile.js"; const d = new Display(); d.show();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script type = "module" src = "App.js"></script> <title>Document</title> </head> <body> <h1>ES6 Cyclic dependencies</h1> </body> </html>
export let count = 0; export function show() { count++; }
import { show, count } from './Mobile.js'; console.log(count); show(); console.log(count);