溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ES6中模塊化怎么用

發布時間:2020-12-05 14:15:51 來源:億速云 閱讀:141 作者:小新 欄目: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女