ES6 export all values from object

前端 未结 9 1787
梦谈多话
梦谈多话 2020-11-28 07:32

Say I have a module (./my-module.js) that has an object which should be its return value:

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

// \"export values\         


        
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 08:36

    Exporting each variable from your variables file. Then importing them with * as in your other file and exporting the as a constant from that file will give you a dynamic object with the named exports from the first file being attributes on the object exported from the second.

    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'
    

提交回复
热议问题