What is the best way to export an object literal with ES6/2015?

前端 未结 3 672
时光说笑
时光说笑 2021-02-05 01:24

Seemingly a very simple task...

export default function() {
    return {
        googleClientID:\'xxxx\'
    }
}

Is it the best way to export o

3条回答
  •  囚心锁ツ
    2021-02-05 01:43

    @madox2 's and @void 's answer may be some kind of common misunderstanding.

    I just ran into a similar problem while issuing a PR to DefinitelyTyped -- #18725. The typescript compiler complains about the generated files.

    A example should be:

    // ...
    import zip from "./zip";
    import zipObject from "./zipObject";
    import zipObjectDeep from "./zipObjectDeep";
    import zipWith from "./zipWith";
    
    export default {
      // ...
      zip,
      zipObject,
      zipObjectDeep,
      zipWith
    };
    

    At the first glance, i didn't think its my problem. Because i just copy the code from lodash-es. But then i can't find any simple approach to remove the errors.

    So I go to the spec for answer. Wow, the spec doesn't talk about default export of an object, if I read it right.

    Conclusion:

    The follow is spec-respected:

    export { googleClientID:'xxxx' }
    

    Just found some more references:

    • https://medium.com/@timoxley/named-exports-as-the-default-export-api-670b1b554f65#a279
    • https://medium.com/@rauschma/note-that-default-exporting-objects-is-usually-an-anti-pattern-if-you-want-to-export-the-cf674423ac38

提交回复
热议问题