温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ES6中模块化怎么用

发布时间:2020-12-05 14:15:51 来源:亿速云 阅读:107 作者:小新 栏目:web开发

小编给大家分享一下ES6中模块化怎么用,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

模块化就是为了使功能单一,把各个耦合性不高的功能抽离出来成单一的模块,每个模块提供单一的功能

export 导出模块;import 导入模块

使用多个export

module.js

export let A = 123;
export function test() {
    return 'test';
}
export class Hello {
    test() {
        console.log('class');
    }
}

index.js

// 1.基本用法
import {A,test,Hello} from './class/module';
console.log(A, test()); // 123 "test"
// 2.只关心某些内容
import {A} from './class/module';
console.log(A); // 123
// 3.* 和 as。* 表示导入所有,as 表示起一个别名
import * as module1 from './class/module';
console.log(module1.test()); // test

使用export default

module.js

// 推荐写法
let A = 123;
let test = function() {
    console.log('test');
};
class Hello {
    test() {
        console.log('class');
    }
}
// default 给导出的对象不起名字,把权力交给引入方
export default {
    A,
    test,
    Hello
}

index.js

import module2 from './class/module';
console.log(module2.A); // 123

看完了这篇文章,相信你对ES6中模块化怎么用有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI