【发布时间】:2018-06-21 16:29:26
【问题描述】:
假设您定义了一个如下所示的变量 (snackbar)。 Angular 类的其余部分可以使用this 访问它,但不能在 Promise 的回调中访问。我正在使用 AngularFire 5。
export class SomeComponent implements OnInit {
constructor(private snackbar: MatSnackBar, private afs: AngularFirestore) {}
//Promise function
this.afs.doc(path).set({
...some code
}).then(function(docRef){
this.snackbar.open('Snackbar message', 'OK'); //Sanckbar unreachable
});
//Regular function
this.snackbar.open('Snackbar message', 'OK'); //Sanckbar works fine
}
有人如何从then 中获取这些变量?还是根本不可能?
【问题讨论】:
-
将
function(docRef)替换为arrow function。您的context已更改
标签: javascript angular firebase callback promise