gpt4 book ai didi

ionic-framework - ionic 4 - 警报 Controller : Property 'present' does not exist - Angular?

转载 作者:行者123 更新时间:2023-12-03 14:50:33 24 4
gpt4 key购买 nike

我正在 Ionic 4 中设置一个新警报 - 空白类型:角度项目。
这是基本警报,但我的项目运行出错。

错误

'Promise' 类型不存在属性 'present'。您是否忘记使用“等待”?

我创建与文档中相同的代码。友情链接:https://ionicframework.com/docs/api/components/alert/AlertController/

我的代码:

import { AuthenticationService } from './../../services/authentication.service';
import { Component, OnInit } from '@angular/core';
import { AlertController, LoadingController, NavController } from
'@ionic/angular';

@Component({
selector: 'app-register',
templateUrl: './register.page.html',
styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
createSuccess = false;
registerCredentials = { email: '', password: '' };

constructor(
private nav: NavController,
private auth: AuthenticationService,
private alertCtrl: AlertController) { }

ngOnInit() {
}

presentAlert() {
const alert = this.alertCtrl.create({
message: 'Low battery',
subHeader: '10% of battery remaining',
buttons: ['Dismiss']
});
alert.present(); <--- error Property 'present' does not exist on type 'Promise<HTMLIonAlertElement>'. Did you forget to use 'await'?
}
public register() {
this.auth.register(this.registerCredentials).subscribe(success => {
if (success) {
this.createSuccess = true;
this.showPopup('Success', 'Account created.');
} else {
this.showPopup('Error', 'Problem creating account.');
}
},
error => {
this.showPopup('Error', error);
});
}

应该工作的 showPopup 功能..
showPopup(title, text) {
let alert = this.alertCtrl.create({
message: title,
subHeader: text,
buttons: [
{
text: 'OK'
}
]
});
alert.present(); <-- the same error
}

最佳答案

您使用的文档是指 ionic 3当您使用时 ionic 4 ,需要引用当前的Ionic 4 docsthis .

this.alertController.create({...})

返回对象的 promise 作为错误指定。

您的代码需要是:
 async presentAlert() {
const alert = await this.alertCtrl.create({
message: 'Low battery',
subHeader: '10% of battery remaining',
buttons: ['Dismiss']
});
await alert.present();
}

关于ionic-framework - ionic 4 - 警报 Controller : Property 'present' does not exist - Angular?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54216298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com