module.exports = { key: "value" } 的 ES6 等价物是什么?
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;
如果你使用export default,那么你不需要使用括号。所以你像这样导入模块:
import module from 'module.js';
// access key property
console.log(module.key)
如果您想像 import {key, key2} from 'module.js';
一样导入模块,请参阅@Michał Perłakowski 的回答。
我有以下代码:
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;
如果你使用export default,那么你不需要使用括号。所以你像这样导入模块:
import module from 'module.js';
// access key property
console.log(module.key)
如果您想像 import {key, key2} from 'module.js';
一样导入模块,请参阅@Michał Perłakowski 的回答。