Javascript:从嵌套函数调用外部方法
我想调用Javascript:从嵌套函数调用外部方法,javascript,typescript,ecmascript-6,ecmascript-5,Javascript,Typescript,Ecmascript 6,Ecmascript 5,我想调用openSnackBar,但出现异常。任何人都可以帮助我如何处理嵌套函数的外部方法调用吗 export class DetailsComponent implements OnInit, OnDestroy { updateTodoPromise.then(function (fulfilled) { // yay, you got a new phone this.openSnackBar('Task saved successfully
openSnackBar
,但出现异常。任何人都可以帮助我如何处理嵌套函数的外部方法调用吗
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双箭头表示法保留
此
范围
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,
});
}
导出类详细信息组件实现OnInit、OnDestroy
{
updateTodoPromise.then(completed=>{/{/使用ES6双箭头表示法来保留这个范围
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,
});
}
导出类详细信息组件实现OnInit、OnDestroy
{
然后(完成=>{/{//