【ES6】export default和import、module.exports和require()使用简析

末蓝、 2022-05-19 00:58 387阅读 0赞

ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使用module.exports导出接口。

es6模块化:export default和import (vue中使用的是这种规范,运行在浏览器端)

commonJS模块化(node):moudle.exports 和require(node后端使用)

export default和import

案例一

写一个Student.js文件

  1. export default class Student{
  2. constructor(name,id){
  3. this.name = name
  4. this.id = id
  5. }
  6. }

写一个app.js文件

  1. import student from "./test.js"
  2. var st1 = new student("fdfdfd ","111")
  3. console.log(st1.name);
  4. console.log(st1.id);

如果直接在编辑器使用点击运行是不行的,因为这是es6的模块化语法,node的模块化语法是module.exports和require

这里运行方式有两种

一:新建html文件引用app.js,type=”module”,是浏览器es6模块化方式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
  10. <script src="./app.js" type="module"></script>

二:使用babel转化,一般在webpack中使用,

在webpack中打开使用devtool;”eval”,方便看到转化代码,下面是webpack.conf.js代码,使用运行webpack打包,使用了babel-loader

  1. const path = require('path');
  2. module.exports = {
  3. entry: './src/app.js',
  4. output: {
  5. path: path.resolve(__dirname, 'dist'),
  6. filename: 'bundle.js'
  7. },
  8. devtool:'eval',
  9. module: {
  10. rules: [
  11. {
  12. test: /\.js$/,
  13. use: {
  14. loader: 'babel-loader',
  15. options: {
  16. presets: [
  17. ['@babel/preset-env',{
  18. targets:{
  19. browsers:['last 2 versions']
  20. }
  21. }]
  22. ]
  23. }
  24. },
  25. exclude: '/node_modules/'
  26. }
  27. ]
  28. }
  29. };

在index.html中引入打包后的dist/bundle.js 即可

案例二:

先写一个a.js使用export default导出一个模块

  1. //a.js
  2. export default {
  3. init:function(){
  4. this.handleAddListener('load',function () {
  5. console.log("页面加载了。。")
  6. })
  7. },
  8. handleAddListener: function (type, fn) {
  9. if (window.addEventListener) {
  10. window.addEventListener(type, fn)
  11. } else {
  12. window.attachEvent('on' + type, fn)
  13. }
  14. }
  15. }

b.js中导入a.js

  1. //b.js
  2. import aabb from './a.js'
  3. aabb.init()

然后使用在通过b.html中引入b.js,type=’module’表示浏览器用es6解析

  1. //b.html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Title</title>
  7. </head>
  8. <body>
  9. </body>
  10. </html>
  11. <script type="module">
  12. import './b'
  13. </script>

70

如果在c.html中直接引入a.js,并且type=”module”也可以打印结果

  1. //c.html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Title</title>
  7. </head>
  8. <body>
  9. </body>
  10. </html>
  11. <script type="module">
  12. import as from './a.js'
  13. as.init();
  14. </script>

70

moudle.exports 和require

创建文件student.js

  1. module.exports = function (name, id) {
  2. return name+id
  3. }

文件app.js

  1. var st1 = require("./student")
  2. console.log(st1("zhangsan", '001'));

这是commonJS规范,运行在node服务端,node支持这种语法,所以可以点击运行直接执行。

补充:

Webpack支持:

AMD(RequireJS)

ES Modules

CommonJS

现在在都在往ESM(es6模块化)方向发展

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读