离子2,如何正确使用警报

yud*_*wan 4 ionic2

我正在使用Ionic 2进行编程,并且我想在单击按钮时显示弹出警报.

这是我home.html中的代码:

<button (click)='openFilters()'>CLICK</button>
Run Code Online (Sandbox Code Playgroud)

在我的家里

import {Component} from '@angular/core';
import {Page, NavController, Alert} from 'ionic-angular';

@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
  constructor(nav: NavController, alertCtrl: AlertController) {

  }

  openFilters() {
    let alert = this.alertCtrl.create({
        title: 'Low battery',
        subTitle: '10% of battery remaining',
        buttons: ['Dismiss']
    });
    alert.present();
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读了关于这个主题的一些stackoverflow问题,并试图像这样实现它:

openFilters() {
    let alert:Alert = Alert.create({
        title: 'Low battery',
        subTitle: '10% of battery remaining',
        buttons: ['Dismiss']
    });
    this.nav.present(alert);
  }
Run Code Online (Sandbox Code Playgroud)

我还没有完成任何事情; 我收到错误.

Jay*_*way 11

一定要导入:

import {AlertController} from 'ionic-angular';
Run Code Online (Sandbox Code Playgroud)

并有这样的代码:

constructor(private alertController: AlertController) {

}


openFilters() {
    let alert = this.alertController.create({
        title: 'Example',
        subTitle: 'Example subtitle',
        buttons: ['OK']
    });

    alert.present();
}
Run Code Online (Sandbox Code Playgroud)

Beta 11已经发生了变化,似乎在线文档尚未更新,您可以随时进入node_modules中的ionic-angular文件夹,找到您尝试使用的组件,以获取更好的文档示例.