【发布时间】:2021-10-26 00:40:00
【问题描述】:
所以在反应中我做了一个按钮来删除一些数据,所以我需要通过 id 获取汽车并使用 react js axios 将其删除,所以我得到的响应是一个空数组,所以请有人帮助我。
代码如下:
在服务数据文件中:
get(id) {
return http.get(`/get/${id}`);
}
delete(id) {
return http.delete(`/delete/${id}`);
}
Component.jsx
this.state = {
idCars: null,
carName: "",
carModel: "",
cars: [],
submitted: false
};
}
getCar(idCars) {
DataService.get(idCars)
.then(response => {
this.setState({
cars: response.data
});
console.log(response.data);
})
.catch(e => {
console.log(e);
});
}
componentDidMount() {
this.getCar(this.props.match.params.idCars);
this.retrieveCars()
}
deleteC() {
DataService.delete(this.state.cars.idCars)
.then(response => {
this.props.history.push('/Classement');
this.refreshList()
})
.catch(e => {
console.log(e);
});
}
render() {
const { cars } = this.state;
return (
<div ><tbody>
{
cars.map(data => (
<tr >
<th scope="row">{data.idCars}</th>
<td>{data.carName}</td>
<td>{data.carModel}</td>
<td>
<button className="btn btn-danger" onClick={this.deleteC}>
Remove
</button>
</td>
</tr>
))
}
并且什么都没有删除,我该如何解决这个问题
【问题讨论】:
标签: reactjs axios crud http-delete