Ionic "await signInWithEmailAndPassword()" 在没有 livereload 的情况下无法在 iOS simulator/device 上工作

Ionic "await signInWithEmailAndPassword()" not working on iOS simulator/device without livereload

在我的 Ionic 5 电容器应用程序中,我有一个调用此函数的按钮

import {
  signInWithEmailAndPassword, signOut,
  User, UserCredential,
  } from '@angular/fire/auth';
    

//...等等等等等等

async signIn(value)
      { 
        try {alert('signing in')
          return (await signInWithEmailAndPassword(this.auth, value.email, value.password)).user
        } catch (error) {
          alert('what the heck?' + error)
        } 
      }

这在 Web 和 Android 上运行良好(它 return 是对象并继续)。在 iOS 模拟器和设备上,它可以使用 livereload,但如果没有 livereload,它什么也不做,甚至 return 什么也做不了。 'signing in' 弹出,但不会从那里继续。

知道为什么会这样吗?

经过一番挣扎,幸运的是我找到了需要做的事情。

在您的 app.component.ts 中添加以下代码:

import { Component } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { initializeApp } from 'firebase/app';
import { indexedDBLocalPersistence, initializeAuth } from 'firebase/auth';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {
  constructor() {
    const app = initializeApp(environment.firebase);
    if (Capacitor.isNativePlatform()) {
      initializeAuth(app, {
        persistence: indexedDBLocalPersistence
      });
    }
  }
}

在您的 app.module.ts 文件中,将以下内容添加到您的 imports 数组。

provideAuth(() => {
      if (Capacitor.isNativePlatform()) {
        return initializeAuth(getApp(), {
          persistence: indexedDBLocalPersistence,
        });
      } else {
        return getAuth();
      }
    }),

并从 @angular/fire/auth.

中导入必要的函数和值

这是与 Puneet Kushway 类似的解决方案。我发现此解决方案更好,因为它使您的 app.component.ts 文件保持整洁。