简体   繁体   中英

Export all objects from javascript object

I have a javascript object that each attribute is a function:

{
  SetBtcPrice: {
    key: 'SetBtcPrice',
    options: { repeat: [Object], backoff: [Object], attempts: 10 },
    handle: [AsyncFunction: handle]
  },
  SetVenezuelanRate: {
    key: 'SetVenezuelaRate',
    options: { repeat: [Object], backoff: [Object], attempts: 10 },
    handle: [AsyncFunction: handle]
  }
}

I'm exporting it as export default { SetBtcPrice, SetVenezuelanRate }; and I'm importing it in another file as import ExchangeRates from "./exchangeRates"; Then in this file I'm exporting the ExchangeRates and another funtion:

exchangeRates.js:

import SetBtcPrice from "./SetBtcPrice";
import SetVenezuelanRate from "./SetVenezuelaRate";

export default { SetBtcPrice, SetVenezuelanRate };

jobs/index.js:

export { default as UserRegistrationMail } from "./UserRegistrationMail";
import ExchangeRates from "./exchangeRates";

export { ExchangeRates };

In another file I'm importing import * as jobs from "../jobs"; , this gives me:

{
  UserRegistrationMail: [Getter],
  ExchangeRates: {
    SetBtcPrice: {
      key: 'SetBtcPrice',
      options: [Object],
      handle: [AsyncFunction: handle]
    },
    SetVenezuelanRate: {
      key: 'SetVenezuelaRate',
      options: [Object],
      handle: [AsyncFunction: handle]
    }
  }
}

How can I deconstruct the ExchangeRates object, so when importing * as jobs it gives me this:

{
  UserRegistrationMail: [Getter],
  SetBtcPrice: {
    key: 'SetBtcPrice',
    options: [Object],
    handle: [AsyncFunction: handle]
  },
  SetVenezuelanRate: {
    key: 'SetVenezuelaRate',
    options: [Object],
    handle: [AsyncFunction: handle]
  }
}

I was wrong

In jobs/index.js, try export { ...ExchangeRates }

Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment


This is REAL SOLUTION

export const { SetBtcPrice, SetVenezuelanRate } = ExchangeRates

export { SetBtcPrice, SetVenezuelanRate } from './exchangeRates'

Your default export is an object with SetBtcPrice and SetVenezualanRate as properties. You can destructure those properties in an export statement.

Importing them will be similar:

import { SetBtcPrice, SetVenezuelanRate } from './some/path'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM