“没有AlertController的提供者!” npm离子角

时间:2018-11-04 19:26:35

标签: angular ionic-framework

以下代码用于ionic应用程序中的自定义启动屏幕组件,在该应用程序中,我已请求服务器获取令牌和身份!使用“离子角”:“ ^ 3.9.2”

import {Component, OnInit} from '@angular/core';
import {BackendService} from "../../services/backend.service";
import {SessionService} from "../../services/session.service";
import {AlertController} from "ionic-angular";

@Component({
    selector: 'app-splash-screen',
    templateUrl: './splash-screen.component.html',
    styleUrls: ['./splash-screen.component.scss']
})

export class SplashScreenComponent implements OnInit {

constructor(private backendService: BackendService,
            private sessionService: SessionService, private alertCtrl: AlertController) {

}

ngOnInit() {

    if (this.sessionService.getBearerToken()) {
        this.requestForIdentity();
    } else {
        this.backendService.Token().subscribe((response: any) => {
            this.sessionService.addAccessToken(response.access_token);
            this.sessionService.addRefreshToken(response.refresh_token);
            this.requestForIdentity();
        }, error => {
            console.log(error)
        })
    }
}

requestForIdentity() {
    this.backendService.GetIdentity().subscribe((response: any) => {
        console.log(response);
        this.showAlert();
    }, error => {
        console.log(error);
    })
}

showAlert() {
    const alert = this.alertCtrl.create({
        title: 'New Friend!',
        subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
        buttons: ['OK']
    });
    alert.present();
}

}

现在,当收到身份响应时,想要显示一个简单的警报!但是根据API文档,我将AlertController注入了SplashScreenComponent并在构建时发生了以下错误!在互联网上找不到任何解决方案。

AlertController Error! No provider for AlertController

SplashScreenComponent位于SplashScreen模块内部,该模块是一个延迟加载的模块。

2 个答案:

答案 0 :(得分:1)

根据我的要求,我使用的是'ionic-angular's AlertController,@ NinjaJami的Answer帮助我更具体地研究了这个问题!

使用“离子角”停止并替换,

import {AlertController} from "ionic-angular";

使用

import {AlertController} from "@ionic/angular";

然后替换

showAlert() {
    const alert = this.alertCtrl.create({
        title: 'New Friend!',
        subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
        buttons: ['OK']
    });
    alert.present();
}

具有:

showAlert() {
    const alert: any = this.alertCtrl.create({
        title: 'New Friend!',
        subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
        buttons: ['OK']
    });

    alert.then((_alert: any)=> {
        _alert.present();
    })
}

说明::this.alertCtrl.create()返回Promise,因此无法直接调用alert.present(),因此,使用alert.then()可以调用present ()函数。

特别感谢@NinjaJami

答案 1 :(得分:0)

确保已导入 IonicModule

 imports:      [ IonicModule.forRoot(),BrowserModule, FormsModule ],

工作演示

https://stackblitz.com/edit/ionic-error?file=src/app/app.component.ts