Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

javascript - 从javascript对象导出所有对象(Export all objects from javascript object)

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

(我有一个JavaScript对象,每个属性都是一个函数:)

{
  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 };

(我将其导出为export default { SetBtcPrice, SetVenezuelanRate };)

and I'm importing it in another file as import ExchangeRates from "./exchangeRates";

(我将其导入到另一个文件中作为import ExchangeRates from "./exchangeRates";)

Then in this file I'm exporting the ExchangeRates and another funtion:

(然后在此文件中,导出ExchangeRates和另一个功能:)

exchangeRates.js:

(exchangeRates.js:)

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

export default { SetBtcPrice, SetVenezuelanRate };

jobs/index.js:

(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";

(在另一个文件中,我将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:

(我该如何解构ExchangeRates对象,因此在将* as jobs导入* as jobs会得到以下信息:)

{
  UserRegistrationMail: [Getter],
  SetBtcPrice: {
    key: 'SetBtcPrice',
    options: [Object],
    handle: [AsyncFunction: handle]
  },
  SetVenezuelanRate: {
    key: 'SetVenezuelaRate',
    options: [Object],
    handle: [AsyncFunction: handle]
  }
}
  ask by Otavio Bonder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was wrong(我错了)

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

(在Jobs / index.js中,尝试export { ...ExchangeRates })

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

(另请参阅https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)


This is REAL SOLUTION(这是真正的解决方案)

export const { SetBtcPrice, SetVenezuelanRate } = ExchangeRates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...