此示例“图像头像” https://material-ui.com/demos/avatars/来自material-ui v3.2.0
网站,但实施时却以某种方式看不到图像。
为什么在
material -ui-v3.2.0
示例中看不到图像 头像“?
在示例'icon avatars'
和'Letter avatars'
中,我可以看到图像。我使用create-react-app 'react-scripts': '^2.0.4', but also tried on 'react-scripts': '^1.1.5'
,但无济于事。
PFB下面的代码:
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
const styles = {
row: {
display: 'flex',
justifyContent: 'center',
},
avatar: {
margin: 10,
},
bigAvatar: {
width: 60,
height: 60,
},
};
function ImageAvatars(props) {
const { classes } = props;
return (
<div className={classes.row}>
<Avatar alt="Remy Sharp" src="/static/images/remy.jpg" className={classes.avatar} />
<Avatar
alt="Adelle Charles"
src="/static/images/uxceo-128.jpg"
className={classNames(classes.avatar, classes.bigAvatar)}
/>
</div>
);
}
ImageAvatars.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ImageAvatars);
答案 0 :(得分:1)
您需要导入它。仅将路径直接传递到src是行不通的。以下是两种在React中渲染图像的方法
使用导入
import uxceo from "/static/images/uxceo-128.jpg";
import remy from "/static/images/remy.jpg";
<Avatar alt="Remy Sharp" src={remy} className={classes.avatar} />
<Avatar
alt="Adelle Charles"
src={uxceo}
className={classNames(classes.avatar, classes.bigAvatar)}
/>
或使用require
<Avatar alt="Remy Sharp" src={require("/static/images/remy.jpg")} className={classes.avatar} />
<Avatar
alt="Adelle Charles"
src={require("/static/images/uxceo-128.jpg")}
className={classNames(classes.avatar, classes.bigAvatar)}
/>