Exporting Objects with the Exports Object

后端 未结 4 1513
野趣味
野趣味 2021-02-02 14:36

Say I have one .js file containing a javascript object. I want to be able to access that object and all of its functionality from another .js file in t

4条回答
  •  遥遥无期
    2021-02-02 14:55

    Of course you can. In my example I use obj to hold my config info. I put it in a file called index.js in config folder. This makes the index the preferred choice to be picked when I import 'config'. I have 2 exports here one for my node and api stuff and the other for my db. You can ignore the first bit where I set the environment.

    const environment = {
      development: {
        isProduction: false
      },
      production: {
        isProduction: true
      }
    }[ process.env.NODE_ENV || 'development' ];
    
    export default Object.assign({
      host: 'localhost',
      port: '3000',
      remoteApi: {
        token: {
          'X-Token': '222222222222222222'
        },
        base: 'https://www.somedomain.com/api'
      }
    }, environment);
    
    export const db = {
      dbHost: 'localhost',
      dbPort: 176178
    };
    

    Calling import config from '../config'; will pick the default one. And if I specify I can get the db export import { db } from '../config';

提交回复
热议问题