gpt4 book ai didi

javascript - react ,未定义这个

转载 作者:行者123 更新时间:2023-11-30 11:39:29 26 4
gpt4 key购买 nike

我从一个子组件调用这个函数:

removeWorkout: function (workoutId) {
return axios.delete('/delete', {
params: {
id: workoutId
}
}
.then(function(response){
this.componentDidMount();
});
},

服务器删除一条记录:

app.delete('/delete/:id?', (req, res) => {
Exr.find({'_id':req.query.id}).remove().exec();
console.log("server deleting")
res.send("done")
});

但是 this.componentDidMount 不起作用,因为它是未定义的。其他功能正常。

最佳答案

这是一个绑定(bind)问题,您需要使用.bind(this) 或使用arrow function 来绑定(bind)context箭头功能 将为您完成这项工作。

使用这个:

.then( (response) => {
this.componentDidMount();
});

.then( function(response) {
this.componentDidMount();
}.bind(this));

关于javascript - react ,未定义这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com