简体   繁体   English

JavaScript 导入而不是 object 中的要求

[英]JavaScript import instead of require in an object

So let's say i have some code like this:所以假设我有一些这样的代码:

module.exports = {
  Someclass: require('./lib/Someclass')
}

I can make it more es6我可以让它更 es6

export default {
  Someclass: require('./lib/Someclass')
}

But how would i replace the require with es6 syntax without setting a variable?但是我如何在不设置变量的情况下用 es6 语法替换 require 呢? Thanks in advance提前致谢

You probably should not default-export an object, but use named exports instead.您可能不应该默认导出 object,而是使用命名导出。 So either所以要么

// re-export the whole namespace
import * as Someclass from './lib/Someclass';
export { Someclass }

or或者

// re-export the default export
export { default as Someclass } from './lib/Someclass';

MDN lists the following patterns for aggregating modules: MDN 列出了以下聚合模块的模式:

export * from './lib/Someclass' // does not set the default export
export * as name1 from './lib/Someclass'
export { name1, name2, …, nameN } from './lib/Someclass'
export { import1 as name1, import2 as name2, …, nameN } from './lib/Someclass'
export { default } from './lib/Someclass'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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