【发布时间】:2019-07-19 13:51:05
【问题描述】:
我试图通过一个组件传递一个道具。它可以工作,但我不能将值放在 axios.get 的 URL 中。 我在另一个组件中有相同的代码,它在那里工作。 希望有人能帮助我。
interface VideoProps {
videoId: number
}
interface CommentState {
comments: any,
videoId: number
}
export default class ShowComments extends React.Component<VideoProps, CommentState>
{
constructor(props: CommentState) {
super(props);
this.state = {
comments: [],
videoId: this.props.videoId,
};
}
componentWillMount(): void {
const videoId = this.props.videoId;
axios.get('/api/get/comments/'+ videoId)
.then((response: any) => {
console.log(response);
this.setState({
comments: response.data
});
});
}
【问题讨论】:
-
不相关,但一般建议在
cDM(不是cWM)中做异步操作。 -
你从道具中得到正确的值吗?
-
我得到了正确的值,是的
-
确保您运行的是您认为的代码。这就是您的操作方式,因此,如果它不起作用,则说明有其他问题在起作用,或者此处实际上并未发生错误。例如,请求将在初始渲染后完成;你处理那个案子吗?
标签: reactjs typescript axios