【问题标题】:How to use material UI default components如何使用材质 UI 默认组件
【发布时间】:2021-12-17 19:22:04
【问题描述】:

我一直在使用bootstrap,我发现material ui也是一个很好的框架,并决定开始学习如何使用它。 在进入 MUI 组件时,我被卡住了。我想知道如何使用默认的 MUI 组件 无需自定义。比如我想复制card component并在浏览器上输出。

例如,其中一份文件的代码是:

import * as React from 'react';
import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent';
import CardMedia from '@mui/material/CardMedia';
import Typography from '@mui/material/Typography';
import { Button, CardActionArea, CardActions } from '@mui/material';

export default function MultiActionAreaCard() {
  return (
    <Card sx={{ maxWidth: 345 }}>
      <CardActionArea>
        <CardMedia
          component="img"
          height="140"
          image="/static/images/cards/contemplative-reptile.jpg"
          alt="green iguana"
        />
        <CardContent>
          <Typography gutterBottom variant="h5" component="div">
            Lizard
          </Typography>
          <Typography variant="body2" color="text.secondary">
            Lizards are a widespread group of squamate reptiles, with over 6,000
            species, ranging across all continents except Antarctica
          </Typography>
        </CardContent>
      </CardActionArea>
      <CardActions>
        <Button size="small" color="primary">
          Share
        </Button>
      </CardActions>
    </Card>
  );
}

在尝试在浏览器上输出相同的组件时,我这样做:

import * as React from 'react';
import Card from '@mui/material/Card';
import CardActions from '@mui/material/CardActions';
import CardContent from '@mui/material/CardContent';
import CardMedia from '@mui/material/CardMedia';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';

const DefaultCard = () => {
  return (
    <Card sx={{ maxWidth: 345 }}>
      <CardMedia
        component=""
        alt="Yes"
        height="140"
        image=""
      />
      <CardContent>
        <Typography gutterBottom variant="h5" component="div">
          Lizard
        </Typography>
        <Typography variant="body2" color="text.secondary">
          Lizards are a widespread group of squamate reptiles, with over 6,000
          species, ranging across all continents except Antarctica
        </Typography>
      </CardContent>
      <CardActions>
        <Button size="small">Share</Button>
        <Button size="small">Learn More</Button>
      </CardActions>
    </Card>
  );
}

export default DefaultCard

但是,我不断收到以下错误

Error in ./~/@mui/material/node/ButtonBase/TouchRipple.js
Module parse failed: C:\dev\frontend\node_modules\@mui\material\node\ButtonBase\TouchRipple.js Unexpected token (257:46)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (257:46)
 @ ./~/@mui/material/node/ButtonBase/ButtonBase.js 34:42-66

Error in ./~/@mui/material/node/styles/createTransitions.js
Module parse failed: C:\dev\frontend\node_modules\@mui\material\node\styles\createTransitions.js Unexpected token (58:40)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (58:40)
 @ ./~/@mui/material/node/styles/createTheme.js 29:48-78

我对错误一无所知。我希望有人帮我指出我做错了什么以及如何消除它

【问题讨论】:

  • 你是如何创建你的 React 应用的?你有没有改变任何配置?
  • 我使用npx create-react-pp . 创建了 React 应用,并且我也安装了 Material UI。我没有更改任何配置。我在App.js 中输出DefaultCard。如果我注释掉包含材料 UI 代码的组件,其他组件将正常运行。但是默认的 mui 组件不断给我上面的错误

标签: javascript reactjs material-ui


【解决方案1】:

尝试删除node_modulespackage-lock.jsonyarn/lock并重新安装

【讨论】:

  • 我之前做过,但错误仍然存​​在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 2021-10-14
  • 1970-01-01
  • 2021-11-11
  • 2020-02-05
  • 1970-01-01
  • 2020-05-12
相关资源
最近更新 更多