Javascript “反应”;这";方法子异步函数中未定义
我有一个组件,在其中我定义了handleClick(onClick处理程序),我想调用异步函数。下面的代码是我所做的,我的问题是:为什么在异步函数()调用中无法访问“this”Javascript “反应”;这";方法子异步函数中未定义,javascript,reactjs,ecmascript-2017,Javascript,Reactjs,Ecmascript 2017,我有一个组件,在其中我定义了handleClick(onClick处理程序),我想调用异步函数。下面的代码是我所做的,我的问题是:为什么在异步函数()调用中无法访问“this” class GameListItem扩展了BaseComponent{ 构造函数(道具、上下文){ 超级(道具、背景); 此.state={ 名称:props.name, 所有者:props.owner, uid:props.uid, 状态:道具状态 }; this.handleClick=this.handleClic
class GameListItem扩展了BaseComponent{
构造函数(道具、上下文){
超级(道具、背景);
此.state={
名称:props.name,
所有者:props.owner,
uid:props.uid,
状态:道具状态
};
this.handleClick=this.handleClick.bind(this);
}
handleClick(){
让inst=这个;
(
异步函数(){
//为什么这里的“this”没有定义?
让res=wait inst.engine.async_action({'action':'join_game','data':{'game_uid':inst.state.uid}});
console.log('res:',res);
}()
);
}
render(){
返回(
);
}
}
已解决。我失去了生活的意义。这项工作:
(
async function(inst) {
let res = await inst.engine.async_action({'action': 'join_game', 'data': {'game_uid': inst.state.uid}});
console.log('res: ', res);
}
)(this);
解决了。我失去了生活的意义。这项工作:
(
async function(inst) {
let res = await inst.engine.async_action({'action': 'join_game', 'data': {'game_uid': inst.state.uid}});
console.log('res: ', res);
}
)(this);
函数不使用
此
。函数不使用此
。