zoukankan      html  css  js  c++  java
  • ES6 模块定义 export 与 import

     

     

     

    1. 一般导出 export

    math.js

     

    export function* getFibo()

    {

    let a = 1;

    let b = 1;

    yield a;

    yield b;

    while (true) {

    let next = a + b;

    a = b;

    b = next;

    yield next;

    }

    }

     

    export function add(a,b)

    {

    return a+b;

    }

     

    index.js

     

    • 注意:如果导出模块没有default定义,那么 引用 导出模块的时候必须有 {} ,标红部分 import {add,getFibo} from "./tool/math.js"

    { } 内容必须与 math定义的值一样,当然也可以用 as

     

    import {add,getFibo} from "./tool/math.js"

     

    let generator = getFibo;

     

    for (let i = 1; i < 10; i++) {

    let result = generator().next().value;

    console.log(`fibo:${i} =${result}`);

    }

     

    console.log(add(1,2));

     

    含有 default 导出 export

     

    math.js

    export default function* getFibo()

    {

    let a = 1;

    let b = 1;

    yield a;

    yield b;

    while (true) {

    let next = a + b;

    a = b;

    b = next;

    yield next;

    }

    }

     

    index.js

     

    import myFibo from "./tool/math.js"

     

    let generator = myFibo;

     

    for (let i = 1; i < 10; i++) {

    let result = generator().next().value;

    console.log(`fibo:${i} =${result}`);

    }

     

     

    • 注意:如果导出模块使用 default 导出,那么 引用 导出模块的时候不需要 {},导出函数的名称可自定义 ,标红部分 import myFibo from "./tool/math.js"

     

     

     

  • 相关阅读:
    configure错误列表供参考
    php和AJAX用户注册演示程序
    php中文汉字截取函数
    阻止a标签点击跳转刷新
    js日期插件
    apache 开启Gzip网页压缩
    查询文章的上下篇Sql语句
    thinkphp简洁、美观、靠谱的分页类
    thinkphp自定义模板标签(二)
    thinkphp自定义模板标签(一)
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/8716388.html
Copyright © 2011-2022 走看看