gpt4 book ai didi

Javascript:从嵌套函数调用外部方法

转载 作者:行者123 更新时间:2023-12-01 01:02:30 26 4
gpt4 key购买 nike

我要调用openSnackBar但我得到异常(exception)。任何人都可以帮助我如何处理嵌套函数的外部方法调用?

export class DetailsComponent implements OnInit, OnDestroy
{
updateTodoPromise.then(function (fulfilled) {
// yay, you got a new phone
this.openSnackBar('Task saved successfully!', 'CLOSE');
})
.catch(function (error) {
// ops, mom don't buy it
console.log(error.message);
this.openSnackBar('Task saved successfully!','CLOSE');
});

}

public openSnackBar(full: string, full2: string) {
this.snackBar.open(full, full2, {
duration: 5000,
});
}

最佳答案

使用 ES6 双箭头表示法来保留 this 范围。

export class DetailsComponent implements OnInit, OnDestroy
{
updateTodoPromise.then( fulfilled => { // <--- here
// yay, you got a new phone
this.openSnackBar('Task saved successfully!', 'CLOSE');
})
.catch( error => { // <--- and here
// ops, mom don't buy it
console.log(error.message);
this.openSnackBar('Task saved successfully!','CLOSE');
});

}

public openSnackBar(full: string, full2: string) {
this.snackBar.open(full, full2, {
duration: 5000,
});
}

关于Javascript:从嵌套函数调用外部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55935287/

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