作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个代码:
export class Page {
constructor(public modalController: ModalController) { }
private async openModal(value) {
const modal = await this.modalController.create({
component: AnotherPage,
componentProps: {
"paramID": 123,
"paramTitle": "Test Title",
"takenPhoto": "null"
}
});
return await modal.present();
}
public async takePicture() {
this.openModel(null); // here's OK
Promise.resolve(this.someMethod()).then(this.openModal); // ERROR!!
}
}
关于方法takePicture
我想在执行 Promise.resolve
后打开一个模式。第一次执行时一切正常,但第二次执行时出现错误。
错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'modalController' of undefined
TypeError: Cannot read property 'modalController' of undefined
at tab2.page.ts:25
at Generator.next (<anonymous>)
at tslib.es6.js:73
at new ZoneAwarePromise (zone-evergreen.js:876)
at Module.__awaiter (tslib.es6.js:69)
at openModal (tab2.page.ts:22)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:34201)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
最佳答案
export class Page {
constructor(public modalController: ModalController) { }
private async openModal(value) {
const modal = await this.modalController.create({
component: AnotherPage,
componentProps: {
"paramID": 123,
"paramTitle": "Test Title",
"takenPhoto": "null"
}
});
return modal.present();
}
public async takePicture() {
await this.openModel(null); // Need to await a promise
const data = await this.someMethod()
await this.openModal(data);
}
}
关于javascript - 正确使用 Promise.resolve().then() 错误 : Uncaught (in promise): TypeError: Cannot read property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709705/
我是一名优秀的程序员,十分优秀!