我很反应,在将数据从一个方法传递到另一个方法时遇到了问题。 这是我的反应语法:
var url = "https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1"
class App extends React.Component{
info(val){
console.log(val)
}
request(){
axios.get(url)
.then(function (response) {
this.info(response)
console.log(response.data);
})
}
render() {
return(
<div>
<h1>axios</h1>
{this.request()}
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById("target"))
我的目标是将响应数据从request
方法传递到info
方法。但是,我收到的错误是"TypeError: Cannot read property 'info' of undefined"
你能帮我辨别一下我错过的东西吗?
答案 0 :(得分:2)
非常常见的问题以及可用于此的许多答案,因此添加答案为社区维基。
这是一个绑定问题,您需要将this
与回调绑定。
.then( (response) => {
更多详情请查看以下答案:Why is JavaScript bind() necessary?