ES6 export all values from object

前端 未结 9 1783
梦谈多话
梦谈多话 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:15

    I can't really recommend this solution work-around but it does function. Rather than exporting an object, you use named exports each member. In another file, import the first module's named exports into an object and export that object as default. Also export all the named exports from the first module using 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
    

提交回复
热议问题