2017-07-21 20 views
0

我是非常新的反应,当涉及到将数据从一个方法传递到另一个方法时,我遇到了问题。 这里是我的反应语法:TypeError:无法读取未定义的属性'信息'

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

绑定问题,检查重复的问题 –

回答

3

非常常见的问题和很多答案都可以提供相同的答案,所以添加的答案与社区wiki相同。

这是一个绑定问题,您需要绑定this与回调。

使用arrow function

.then((response) => { 

有关详情,请这样的回答:Why is JavaScript bind() necessary?

相关问题