【发布时间】:2016-10-09 09:04:17
【问题描述】:
下面的方法很好用
var DBBox = React.createClass({
loadArticlesFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
data: {username:data.username,isPublished:data.isPublished, heading:data.heading},
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
但是如果我像这样在第 2 行将方法声明更改为箭头函数
loadArticlesFromServer: ()=> { //error - Cannot read property 'props' of undefined at line 6
或
loadArticlesFromServer= ()=> { //Syntax error
我是否错误地使用了箭头功能或遗漏了什么?还是不支持?我正在使用 chrome 并尝试启用和谐标志,但没有任何运气。
【问题讨论】:
-
您应该研究箭头函数中
this的行为。您不能将bind与箭头一起使用。 -
见this question(没有双关语)
-
感谢@Redu,为我指明了正确的方向。
标签: javascript node.js reactjs ecmascript-6