[英]How to import material-ui components?
Heavily using material-ui in my app, is there a way to avoid doing imports in each app component like this?在我的应用程序中大量使用 material-ui,有没有办法避免像这样在每个应用程序组件中进行导入?
import Typography from "@material-ui/core/Typography";
import Box from "@material-ui/core/Box";
import Grid from "@material-ui/core/Grid";
import Container from "@material-ui/core/Container";
....
or this:或这个:
import {Typography, Box, Grid, Container} from "@material-ui/core";
Is there such thing like this?有这样的事吗? So that I don't need to import each component?
这样我就不需要导入每个组件了吗?
import * from "@material-ui/core"
Thanks in advance: :D先谢谢了
Yes, there is an import all in JavaScript. You can do it like so:是的,JavaScript 中有一个 all 导入。您可以这样做:
import * as Mui from '@material-ui/core';
This puts all of the named exports of @material-ui/core
into the Mui
"namespace".这会将
@material-ui/core
的所有命名导出放入Mui
“命名空间”。 Then, you can easily access any of the components ie:然后,您可以轻松访问任何组件,即:
<Mui.Typography>Test</Mui.Typography>
You can read more about import
here .您可以在此处阅读有关
import
的更多信息。
The first option is not much clean from an import statement perspective, especially when you want to import and use a lot of Mui
components, but as you use path imports to avoid pulling in unused modules, gets an optimized bundle size automatically.从导入语句的角度来看,第一个选项不是很干净,尤其是当您想要导入和使用大量
Mui
组件时,但是当您使用路径导入来避免引入未使用的模块时,会自动获得优化的包大小。
The second option ( import * from "@material-ui/core"
), on the other hand, is the most convenient from a development perspective, and also makes you have a clean import statement, but will make your application packages larger than they import each component separately depending on what part of components you are using.另一方面,第二个选项(
import * from "@material-ui/core"
)从开发的角度来看是最方便的,也使您拥有干净的导入语句,但会使您的应用程序包比它们大根据您使用的组件的哪一部分分别导入每个组件。 Moreover, there is a large scale application you need to import from different sources of Material-ui:此外,还有一个大型应用程序需要从 Material-ui 的不同来源导入:
import {} from '@material-ui/core';
import {} from '@material-ui/icons';
import {} from '@mui/material';
A better optimized approach, is to create a single file in your project where you import each component that you use individually, then export them all under a single namespace:一种更好的优化方法是在您的项目中创建一个文件,您可以在其中导入您单独使用的每个组件,然后将它们全部导出到一个命名空间下:
// src/mui/index.js
export { default as MenuItem } from '@material-ui/core/MenuItem';
export { default as TextField } from '@material-ui/core/TextField';
export { default as Select } from '@material-ui/core/Select';
export { default as ClearIcon} from '@material-ui/icons/Clear';
export { default as FormControl } from '@material-ui/core/FormControl';
export { default as Button } from '@mui/material/Button';
...
Then you can import that file wholesale into each component where you need it:然后您可以将该文件批量导入到您需要的每个组件中:
// ../../MyComponent.js
import {
MenuItem,
TextField,
Select,
ClearIcon,
FormControl
} from 'mui';
Now you have a clean import statement and optimized bundle size as well.现在您有一个干净的导入语句和优化的包大小。
Working with absolute path : I never address components with a relative path (ei./../../mui).使用绝对路径:我从不使用相对路径 (ei./../../mui) 来处理组件。 you can take a look here if you don't know how to use absolute path in React.
如果你不知道如何在 React 中使用绝对路径,你可以看看这里。
Hi you can do this in following way:您好,您可以通过以下方式做到这一点:
export {default as Button} from "@material-ui/core/Button";
从“@material-ui/core/Button”导出{默认为按钮};
export {default as Card} from "@material-ui/core/Card";
从“@material-ui/core/Card”导出{default as Card};
export {default as Paper} from "@material-ui/core/Paper";
从“@material-ui/core/Paper”导出{默认为Paper};
import * as collections from './collections';
从“./collections”导入 * 作为 collections;
Your component file will be as:您的组件文件将是:
import React, {Component} from "react";
import * as collections from './collections';
class Box extends Component {
render() {
return (
<div>
<collections.Button>
Test
</collections.Button>
<collections.Card>test</collections.Card>
<collections.Paper>test</collections.Paper>
</div>
);
}
}
export default Box;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.