zoukankan      html  css  js  c++  java
  • 有关es6的模块化

    假如有两个文件:app.js和config.js

    app.js为主文件要去引用config这个模块

    以前学习node时使用的模块导出:

    需求:导出三个成员,分别是 foo: bar f: function num: 10
    
    exports.foo = 'bar'
    exports.f = function () {
      
    }
    exports.num = 10
    

    es6中的模块导出

    方法一

    export const foo = 'bar'
    export const f = function () {
      
    }
    export const num = 10
    

    两种可以混合使用
    方法二

    const foo = 'bar'
    const f = function () {
      
    }
    const num = 10
    
    export {
      foo,
      f,
      num
    }
    

    通过 export 导出的成员必须通过解构赋值按需加载
    或者通过import * as 变量名 的形式加载所有通过 export 关键字导出的接口成员
    通过 export default 加载导出的成员必须通过 import 变量名 from '模块标识' 进行加载
    export 和 export default 可以共存

    import config from './config'
    import { foo, num } from './config'
    import * as config from './config'
    import { foo } from './config'
    

    默认导出

    config.js

    // 相当于 module.exports = function () {}
    export default function () {
      console.log('fff')
    }
    

    app.js:

    // 这种方式会去找被加载模块中通过 export default 导出的成员
    import defaultConfig from './config'
    

    defaultConfig任意指定

    最后通过模板字符串将它们打印出来

    console.log(default: ${defaultConfig}, foo: ${foo}, all: ${allConfig})

  • 相关阅读:
    Codeforces 985G. Team Players
    关于Hall定理的学习
    bzoj 4561: [JLoi2016]圆的异或并
    UOJ #188. 【UR #13】Sanrd
    LOJ #6053. 简单的函数
    Codeforces F. Cowmpany Cowmpensation
    POJ 3710:Matrix Power Series
    codeforces533E
    luogu2885
    codeforces722E
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12230863.html
Copyright © 2011-2022 走看看