我通过构建一个运动跟踪器来练习我的MERN技能,然后我遇到了这样的错误:"Uncaught :无法读取未定义的属性(阅读'params')"。最近,我找到了一个指导我使用"useParams“钩子的解决方案。但这对我来说没有用,因为我使用的是类组件。下面是造成问题的代码块:
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css"
import axios from "axios";
import { Params } from "react-router-dom";
export default class EditExercises extends Component {
constructor(props) {
super(props);
this.onChangeUsername = this.onChangeUsername.bind(this);
this.onChangeDescription = this.onChangeDescription.bind(this);
this.onChangeDuration = this.onChangeDuration.bind(this);
this.onChangeDate = this.onChangeDate.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
username: "",
description: "",
duration: 0,
date: new Date(),
users: []
}
}
componentDidMount() {
axios.get("http://localhost:5000/exercises/" + this.props.match.Params.id)
.then(response => {
this.setState({
username: response.data.username,
description: response.data.description,
duration: response.data.duration,
date: new Date(response.data.date)
})
})
.catch(error => console.log(error));
发布于 2022-02-28 14:05:19
您的路线有问题,请您共享该文件。它有路线:id在里面吗?像这样
如果没有,请添加它,因为路由器在那里找不到它。如果在那里,请将Params的名称改为params (小写的)。
发布于 2022-02-28 14:05:30
它必须是对线而不是傍线。
axios.get("http://localhost:5000/exercises/" + this.props.match.params.id)
https://stackoverflow.com/questions/71296161
复制相似问题