我要调用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/