angular - 无法读取未定义的属性“”
问题描述
我目前正在尝试在执行 loadModalDialog 后加载模式。我试图在 then 部分中调用它。我收到一个错误
Cannot read property 'dialog' of undefined
如果我以以下方式调用,我不会收到上述错误,但会收到与 userservice 中的依赖项相关的错误,因为 loadModalDialog 启动了对协议组件的调用,该组件查看来自 userService 执行的值。因此,我需要确保在执行 userService 之后需要调用 this.loadModalDialog()。我需要将 this.dialog 的实例传递给函数 loadModalDialog 吗?
this.userService.load();
this.loadModalDialog();
零件
constructor( public dialog: ModalDialogService ) {
export class AppComponent implements OnDestroy {
this.userService.load().then(this.loadModalDialog);
}));
}
private loadModalDialog() {
const ref = this.dialog.open(AgreementComponent, {
// size: 'large'
});
ref.afterClosed.subscribe(result => {
console.log('3rd dialog closed', result);
});
}
解决方案
您正在删除将this
函数传递给.then
语句时的上下文。
您的错误消息是一个危险信号,这this
不是您认为this
应该的。它应该和.bind(this)
你的this.loadModalDialog
函数一样简单。
this.userService.load().then(this.loadModalDialog.bind(this));
或者
this.userService.load().then(() => this.loadModalDialog());
推荐阅读
- python - 从另一个线程或进程更新 Gtk.ProgressBar
- python - 如何在 TensorFlow 中使用 Eager Execution 进行保存和恢复?
- javascript - 如何在 Java 中中止文件下载?
- azure - Azure 功能:队列触发器删除消息,但不触发
- wpf - 在功能区内的控件中正确获取图像大小
- android - RxJava2(Android):PublishSubject 只向活动发出一次?
- python - 在python循环中获取href链接和文本
- android - 事件处理在微调器和按钮的角色中扮演什么角色?
- python - Python添加密码以修改功能
- android - 我无法在片段上设置 ClickListener RelativeLayout