如何要求模块进行多次导出而不命名所有导出?

发布于 2025-01-15 14:22:00 字数 566 浏览 5 评论 0原文

我需要从模块中进行大量导出,如下所示:

employee.js

// Some logic goes here
module.exports = {
    AvgEmployees,
    AvgDailyEmployee,
    AvgAbsenceEmployee,
    AvgWorkDaysEmpolyee,
    ...
    ..
    ..
};

ma​​in.js

const {
  AvgEmployees,
  AvgDailyEmployee,
  AvgAbsenceEmployee,
  AvgWorkDaysEmployee,
  ...
  ...
  ...
} = require('src/employee.js');

// Do what's needed with all the imports
// ....
// ...

我导入了 50 多个导入,每次都开始困扰我命名所有进口。

是否可以一次导入所有这些而不具体命名每个?

I'm requiring a lot of exports from a module as follows:

employee.js

// Some logic goes here
module.exports = {
    AvgEmployees,
    AvgDailyEmployee,
    AvgAbsenceEmployee,
    AvgWorkDaysEmpolyee,
    ...
    ..
    ..
};

main.js

const {
  AvgEmployees,
  AvgDailyEmployee,
  AvgAbsenceEmployee,
  AvgWorkDaysEmployee,
  ...
  ...
  ...
} = require('src/employee.js');

// Do what's needed with all the imports
// ....
// ...

I'm importing more than 50 imports and it starts to bother me every time to name all the imports.

Is it possible to import all of them at once without naming each one specifically ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2025-01-22 14:22:00

是的,像这样:

const employee = require('src/employee.js');

// Do what's needed with all the imports
employee.AvgEmployees
employee.AvgDailyEmployee
// ....
// ...

变量employee是module.exports的值,而const { ... } = require('src/employee.js');只是对象解构:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Yes, like this:

const employee = require('src/employee.js');

// Do what's needed with all the imports
employee.AvgEmployees
employee.AvgDailyEmployee
// ....
// ...

Variable employee is the value of module.exports and const { ... } = require('src/employee.js'); is just object destructuring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文