简体   繁体   English

多重导入 ES6

[英]Multiple import ES6

I have next situation - i'm using library material-ui;我有下一个情况 - 我正在使用图书馆材料-ui; And don't want each time use straight way to node_modules for importing required me component '@material-ui/core/Tabs' .并且不希望每次都直接使用node_modules来导入所需的组件'@material-ui/core/Tabs' I decided to use proxy-file which import all component and then export them for use more comfortable use import MU componentns import - something line import Button from 'components/Button' .我决定使用proxy-file ,它导入所有组件,然后导出它们以便更舒适地使用 import MU componentns import - something line import Button from 'components/Button' I would love to use flexible import from array list.我很想从数组列表中使用灵活的导入。 Example code:示例代码:

<code>
const materialComponents = [
  Button
];

materialComponents.forEach(item=>{
  import item from `@material-ui/core/${item}`
});


export default {
  ...materialComponents
}
</code>

So...I have problem with import item from @material-ui/core/${item} Compiler says that i need use string for route.所以......我有import item from @material-ui/core/${item}问题编译器说我需要使用字符串作为路由。

How can i resolve this problem?我该如何解决这个问题?

Thanks.谢谢。

If I understand your goal correctly, you can achieve the same thing without an additional file.如果我正确理解你的目标,你可以在没有额外文件的情况下实现同样的目标。

Either:任何一个:

import { Button, Card, Icon } from '@material-ui/core'

And then use the components as usual, or use the 'import as' syntax to import all components under a common namespace, ie:然后像往常一样使用组件,或者使用'import as'语法导入公共命名空间下的所有组件,即:

import * as components from '@material-ui/core'

You can then use the imported components by invoking <components.Button> etc.然后,您可以通过调用<components.Button>等来使用导入的组件。

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

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