上传图片的时候,onchange 上传文件改变时的状态的方法,报这个错误,this指向改变,用箭头函数
export default class ModalFrom extends Component {
constructor(props) {
super(props);
this.params = {
name: 'picture',
action: '/api/leju/admin/material/uploadAndSaveImg',
};
this.state = {
imgSrc: '',
};
}
handleChange = (info) => {
console.log(info, 'info===');
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList);
}
if (info.file.status === 'done') {
this.setState({
imgSrc:info.fileList[0].response.ossUrl
})
} else if (info.file.status === 'error') {
message.error(`上传失败,请重新上传`);
}
};
render() {
return (
<div>
<Form onFinish={onFinish}>
<Form.Item name="img" label="图片" rules={[{ required: true, message: '请上传图片' }]}>
<Upload {...this.params} onChange={this.handleChange}>
<Button size="small" type="primary">
上传图片
</Button>
</Upload>
<img src={this.state.imgSrc} art=""></img>
</Form.Item>
</Form>
</div>
);
}
}