ES6 导出对象的所有值

ES6 export all values from object

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

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

// "export values" results in SyntaxError: Unexpected token

所以我可以像这样导入它们:

import {a} from './my-module'           // a === 1
import * as myModule from './my-module' // myModule.a === 1

我发现的唯一方法是对导出进行硬编码:

export let a = values.a
export let b = values.b
export let c = values.c
// or:
export let {a, b, c} = values

这不是动态的。

是否可以导出一个对象的所有值?

好像不是。引自 ECMAScript 6 modules: the final syntax:

You may be wondering – why do we need named exports if we could simply default-export objects (like CommonJS)? The answer is that you can’t enforce a static structure via objects and lose all of the associated advantages (described in the next section).

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

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

export default config;

你可以这样做

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

console.log(config.x); // CHANGE_ME

请注意,这是使用 Typescript。

export const a = 1;
export const b = 2;
export const c = 3;

这将在今天与 Babel transforms 一起工作,并且只要该功能实际登陆浏览器,就应该利用 ES2016 模块的所有优势。

您还可以添加 export default {a, b, c};,这将允许您将所有值作为对象导入 w/o * as,即 import myModule from 'my-module';

来源:

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

从第一个模块导出所有命名导出

values/value.js

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

export {a, b, c};

values/index.js

import * as values from './value';

export default values;
export * from './value';

index.js

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

console.log(values, a); // {a: 1, b: 2, c: 3} 1

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

// 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);

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

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

export { values }; // you could use default, but I'm specific here

然后你可以在 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

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

每个答案都需要更改导入语句。

如果您希望能够使用:

import {a} from './my-module'           // a === 1
import * as myModule from './my-module' // myModule.a === 1

如问题所示,在您的 my-module 中,您拥有需要在一个对象中导出的所有内容(这可能很有用,例如,如果您想使用 Joi 或 [=28= 验证导出的值] 模式)那么你的 my-module 必须是:

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

或者:

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

不漂亮,但可以编译成你需要的。

参见:Babel example

你可以用 javascript 做很多蠢事。我将在这里引用 YDKJS 书中的这句话。

提到的书页 ->

https://books.google.com.tr/books?id=iOc6CwAAQBAJ&pg=PT150&lpg=PT150&dq=JS+engine+cannot+statically+analyze+the+contents+of+plain+object&source=bl&ots=7v8fMUgwhx&sig=dP3BpY7mEvpvfyxO_koWaXczBWI&hl=en&sa=X&ved=2ahUKEwi4qseXyrDdAhUS-6QKHZYTAEQQ6AEwAHoECAEQAQ#v=onepage&q=JS%20engine%20cannot%20statically%20analyze%20the%20contents%20of%20plain%20object&f=false

正在从您的变量文件中导出每个变量。然后像在其他文件中一样使用 * 导入它们,并从该文件中导出常量,将为您提供一个动态对象,其中第一个文件中的命名导出是从第二个文件中导出的对象的属性。

Variables.js

export const var1 = 'first';
export const var2 = 'second':
...
export const varN = 'nth';

Other.js

import * as vars from './Variables';

export const Variables = vars;

Third.js

import { Variables } from './Other';

Variables.var2 === 'second'

为什么不只对对象进行命名导出:

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

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

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

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

let foo = values.a
let { a, b, c } = values

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

let foo = myModule.a
let { a, b, c } = myModule

也可以进行默认导出:

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

export default { a: 1, b: 2, c: 3 }

然后消费它:

import whateverIcallIt from './my-Module'

let foo = whateverIcallIt.a
let {a, b, c } = whateverIcallIt

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

export const a = 1
export const b = 2
//...

甚至

export const a = 1,
             b = 2,
             c = 3,
//...

然后单独导入:

import { a, b, c } from './my-module'