【问题标题】:Ionic 2 Project - Promise issue with authenticationIonic 2 Project - 身份验证的承诺问题
【发布时间】:2017-12-07 10:23:05
【问题描述】:

我一直在寻找解决我遇到的这个问题的方法,但没有找到任何适合我的特殊情况的方法。我正在开发一个 Ionic 2 应用程序,并且正在为我的登录屏幕开发一些身份验证内容。该文件的目的是为我编写的前端代码添加后端功能(请参阅下面 github 链接上的我的 login.html 文件)。我正在关注这个网站上的教程: https://devdactic.com/login-ionic-2/

我遇到的问题是,对于某些将在我的代码中标记的函数,我得到一个“从(函数)返回的承诺被忽略”,这会阻止代码完全运行。它不是编译错误,而是运行时错误:

运行时错误 未捕获(承诺):错误:没有 Http 提供者! d处的错误

我真的不确定这意味着什么,所以任何见解都会非常有帮助。如果需要,我可以提供完整错误的图片。

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  loading: Loading;
  registerCredentials = { email: '', password: ''};

  constructor(public navCtrl: NavController, private auth: AuthService, public navParams: NavParams,
  private alertCtrl: AlertController, private loadingCtrl: LoadingController) {}

  public createAccount() {
    this.navCtrl.push('RegisterPage'); //Promise ignored error
  }

  public login() {
    this.showLoading();
    this.auth.login(this.registerCredentials).subscribe(allowed => {
        if (allowed) {
          this.navCtrl.setRoot('HomePage'); //Promise ignored error
        } else {
          this.showError("Access Denied");
        }
      },
      error => {
        this.showError(error);
      });
  }

  showLoading() {
    this.loading = this.loadingCtrl.create({
      content: 'Please wait...',
      dismissOnPageChange: true
    });
    this.loading.present(); //Promise ignored error
  }

  showError(text) {
    this.loading.dismiss(); //Promise ignored error

    let alert = this.alertCtrl.create({
      title: 'Fail',
      subTitle: text,
      buttons: ['OK']
    });
    alert.present(prompt); //Promise ignored error
  }
}

错误图片:

我只是不确定是什么导致了这个问题,它导致我的代码完全崩溃,甚至无法加载我的 UI。如果你需要查看其他一些文件,其余的都在我的 github 上,这个特定的文件位于 /src/pages/login

其余文件: https://github.com/jparr721/Badmeat/tree/master/BadMeat-Ionic/FirstBuild/src

  • IDE:JetBrains WebStorm
  • 操作系统:MacOS Sierra
  • 离子版本:2.2.2

【问题讨论】:

    标签: javascript angular ionic2


    【解决方案1】:

    我不确定这是否能解决您的问题,但请尝试将以下内容添加到您的 app.module.ts 文件中

    import { HttpModule } from '@angular/http';
    

    它可能会修复您的 http 提供程序错误 您还必须将其添加到同一文件中的导入中

    imports: [
        BrowserModule,
        HttpModule,
        CloudModule.forRoot(cloudSettings),
        IonicModule.forRoot(MyApp),
        IonicStorageModule.forRoot()
      ],
    

    【讨论】:

    • 只有一些小东西,但在您的 auth-service.ts 中,您缺少一个空间构造函数(publichttp: Http){ console.log('Hello AuthService Provider'); }
    • 另外您在 Login.ts 中缺少 RegisterPage 导入,添加 import { RegisterPage } from '../register/register';
    • 我还没有完成登录页面,您认为这可能导致问题吗?
    • 它肯定会引起问题,毫无疑问,它是否能解决你所有的问题我不确定,我只是缩小问题范围..我在 ionic beta 所以我实际上无法运行你的项目自己调试它,所以我只是按照我能看到的去做
    【解决方案2】:

    您可能已经想通了,但请尝试在此处将代码中的公共更改为私有:

    constructor(public navCtrl: NavController,
    

    尝试:

    constructor(private navCtrl: NavController,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 2018-01-31
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多