ES6从对象导出所有值

mau*_*uvm 94 module export ecmascript-6

假设我有一个module(./my-module.js),它有一个应该是其返回值的对象:

let values = { a: 1, b: 2, c: 3 }

// "export values" results in SyntaxError: Unexpected token
Run Code Online (Sandbox Code Playgroud)

所以我可以导入它们:

import {a} from './my-module'           // a === 1
import * as myModule from './my-module' // myModule.a === 1
Run Code Online (Sandbox Code Playgroud)

我发现的唯一方法是硬编码出口:

export let a = values.a
export let b = values.b
export let c = values.c
// or:
export let {a, b, c} = values
Run Code Online (Sandbox Code Playgroud)

哪个不是动态的.

是否可以从对象中导出所有值?

rya*_*ffy 70

我不能真正推荐这种解决方案,但它确实有效.您可以使用命名导出每个成员,而不是导出对象.在另一个文件中,将第一个模块的命名导出导入到对象中,并将该对象导出为默认值.还使用导出第一个模块中的所有命名导出export * from './file1';

值/ value.js

let a = 1;
let b = 2;
let c = 3;

export {a, b, c};
Run Code Online (Sandbox Code Playgroud)

值/ index.js

import * as values from './value';

export default values;
export * from './value';
Run Code Online (Sandbox Code Playgroud)

index.js

import values, {a} from './values';

console.log(values, a); // {a: 1, b: 2, c: 3} 1
Run Code Online (Sandbox Code Playgroud)

  • 您为什么不推荐这个? (2认同)
  • 也许是因为治愈方法比疾病更糟糕(除非您正在编写一个供公众使用的图书馆,并且您对它的输入方式确实很挑剔)? (2认同)

Joe*_*ard 33

似乎不是这样.从ECMAScript 6模块引用:最终语法:

您可能想知道 - 如果我们可以简单地默认导出对象(如CommonJS),为什么我们需要命名导出?答案是您不能通过对象强制执行静态结构并失去所有相关优势(在下一节中描述).

  • 如果它们具有名称/值对,可以使用数组吗? (3认同)

小智 12

尝试这个丑陋但可行的解决方案:

// use CommonJS to export all keys
module.exports = { a: 1, b: 2, c: 3 };

// import by key
import { a, b, c } from 'commonjs-style-module';
console.log(a, b, c);
Run Code Online (Sandbox Code Playgroud)


mik*_*see 8

我只需要为配置文件执行此操作.

var config = {
    x: "CHANGE_ME",
    y: "CHANGE_ME",
    z: "CHANGE_ME"
}

export default config;
Run Code Online (Sandbox Code Playgroud)

你可以这样做

import { default as config } from "./config";

console.log(config.x); // CHANGE_ME
Run Code Online (Sandbox Code Playgroud)

这是使用Typescript记住你.

  • 你应该可以从'./config';`进行`import config (26认同)

spe*_*ode 8

为什么不直接执行对象的命名导出:

let values = { a: 1, b: 2, c: 3 }
export { values }
Run Code Online (Sandbox Code Playgroud)

或者

export let values = { a: 1, b: 2, c: 3 }
Run Code Online (Sandbox Code Playgroud)

然后在需要的地方进行命名导入:

import { values } from './my-module'

let foo = values.a
let { a, b, c } = values
Run Code Online (Sandbox Code Playgroud)

或者

import { values as myModule } from './my-module'

let foo = myModule.a
let { a, b, c } = myModule
Run Code Online (Sandbox Code Playgroud)

也可以进行默认导出:

let values = { a: 1, b: 2, c: 3 }
export default values 
Run Code Online (Sandbox Code Playgroud)

或者

export default { a: 1, b: 2, c: 3 }
Run Code Online (Sandbox Code Playgroud)

然后消费它:

import whateverIcallIt from './my-Module'

let foo = whateverIcallIt.a
let {a, b, c } = whateverIcallIt
Run Code Online (Sandbox Code Playgroud)

如果你想导出一堆单独的值,比如一堆常量,你可以:

export const a = 1
export const b = 2
//...
Run Code Online (Sandbox Code Playgroud)

甚至

export const a = 1,
             b = 2,
             c = 3,
//...
Run Code Online (Sandbox Code Playgroud)

然后单独导入它们:

import { a, b, c } from './my-module'
Run Code Online (Sandbox Code Playgroud)


RiZ*_*KiT 5

我建议如下,让我们期待一个module.js

const values = { a: 1, b: 2, c: 3 };

export { values }; // you could use default, but I'm specific here
Run Code Online (Sandbox Code Playgroud)

然后你可以在index.js 中做:

import { values } from "module";

// directly access the object
console.log(values.a); // 1

// object destructuring
const { a, b, c } = values; 
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3

// selective object destructering with renaming
const { a:k, c:m } = values;
console.log(k); // 1
console.log(m); // 3

// selective object destructering with renaming and default value
const { a:x, b:y, d:z = 0 } = values;
console.log(x); // 1
console.log(y); // 2
console.log(z); // 0
Run Code Online (Sandbox Code Playgroud)

更多解构对象的例子:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring