简体   繁体   English

如何在axios.get函数中传递参数?

[英]How can i pass a param in the axios.get function?

I've tried to pass a prop through a component. 我试图通过一个组件传递道具。 It works but I can't put the value in the URL of axios.get. 它可以工作,但是我不能将值放在axios.get的URL中。 I have the same code in the other Component, and it works there. 我在其他组件中有相同的代码,并且在那里工作。 Hope somebody can help me. 希望有人能帮助我。

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
            });
        });
}
    axios.get(`/api/get/comments/${videoId}`, {
      params: {
        param1: 'param1',
      }
    })
    .then((response: any) => {
        console.log(response);
        this.setState({
            comments: response.data
        });
    });

Also, this won't work: 另外,这将不起作用:

this.state = {
    comments: [],
    videoId: this.props.videoId,
};

this.props should be undefined in the constructor, use props.videoId , not sure if this may cause an error. this.props应该在构造函数中未定义,请使用props.videoId ,不确定是否会导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM