簡體   English   中英

ES6等效於module.exports = {鍵:“值”}的含義是什么?

[英]What is ES6 equivalent of module.exports = { key: “value” }?

我有以下代碼:

module.exports  = { 
    key: "value",
    key2: 1234
}

如果我將其更改為:

export default {
    key: "value",
    key2: 1234
}

然后,以下導入將停止工作:

import {key, key2} from 'module.js';

ES6等同於導出對象?

您可以首先定義變量並導出它們:

const key = 'value';
const key2 = 1234;

export { key, key2 };

或者,您可以將它們導出到定義它們的同一行中:

export const key = 'value';
export const key2 = 1234;

如果使用導出默認值,則無需使用括號。 因此,您可以像這樣導入模塊:

 import module from 'module.js'; // access key property console.log(module.key) 

如果您想import {key, key2} from 'module.js';導入模塊,例如import {key, key2} from 'module.js'; ,請參閱@MichałPerłakowski答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM