在我的 Ionic Angular 应用程序中,我将使用 firebase 进行存储和身份验证。
我建立了一个非常基本的项目,并尝试让所有 Firebase 模拟器正常工作。 按照 firebase 指示运行所有命令并安装 Angular Fire 后,除了存储模拟器之外,我的所有模拟器都工作正常。存储模拟器似乎没有正确连接,因为生产存储桶用于所有内容。
我多次检查了端口并尝试了连接模拟器的不同方法,但似乎没有任何效果
这些是我的文件
storage.service.ts
:
import { Injectable } from '@angular/core';
import { FirebaseApp } from '@angular/fire/app';
import { getBlob, ref } from '@angular/fire/storage';
import { getStorage } from 'firebase/storage';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private storageReference: string = "my-reference";
private storage = getStorage(this.app);
constructor(private app: FirebaseApp) {}
downloadImage(dirRef: string) {
return getBlob(ref(this.storage, `${this.storageReference}/${dirRef}/image1.jpg`))
.then(image => {
var urlCreator = window.URL || window.webkitURL;
return urlCreator.createObjectURL(image);
})
}
}
app.module.ts
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { getAuth, provideAuth } from '@angular/fire/auth';
import { getAnalytics, provideAnalytics, ScreenTrackingService, UserTrackingService } from '@angular/fire/analytics';
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
import { StorageModule, connectStorageEmulator, getStorage, provideStorage } from '@angular/fire/storage';
import { environment } from 'src/environments/environment';
import { USE_EMULATOR as USE_AUTH_EMULATOR } from '@angular/fire/compat/auth';
import { USE_EMULATOR as USE_FIRESTORE_EMULATOR } from '@angular/fire/compat/firestore';
import { USE_EMULATOR as USE_STORAGE_EMULATOR } from '@angular/fire/compat/storage';
import { FIREBASE_OPTIONS } from '@angular/fire/compat';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
provideFirebaseApp(() => initializeApp(environment.firebase.config)),
provideAuth(() => getAuth()),
provideAnalytics(() => getAnalytics()),
provideFirestore(() => getFirestore()),
provideStorage(() => getStorage()),
],
providers: [
{ provide: USE_AUTH_EMULATOR, useValue: !environment.production ? ['http://localhost:9099'] : undefined },
{ provide: USE_FIRESTORE_EMULATOR, useValue: !environment.production ? ['localhost', 8080] : undefined },
{ provide: USE_STORAGE_EMULATOR, useValue: !environment.production ? ['localhost', 9199] : undefined },
{ provide: FIREBASE_OPTIONS, useValue: environment.firebase.config },
{
provide: RouteReuseStrategy,
useClass: IonicRouteStrategy
},
ScreenTrackingService,
UserTrackingService
],
bootstrap: [AppComponent],
})
export class AppModule {}
firebase.json
:
{
"hosting": {
"source": ".",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"frameworksBackend": {
"region": "europe-west1"
}
},
"emulators": {
"auth": {
"port": 9099
},
"firestore": {
"port": 8080
},
"storage": {
"port": 9199
},
"ui": {
"enabled": true
},
"singleProjectMode": true
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"storage": {
"rules": "storage.rules"
}
}
package.json
:
{
"name": "ALaCard",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"lint": "ng lint"
},
"private": true,
"dependencies": {
"@angular/animations": "^17.0.2",
"@angular/common": "^17.0.2",
"@angular/compiler": "^17.0.2",
"@angular/core": "^17.0.2",
"@angular/fire": "^17.0.0",
"@angular/forms": "^17.0.2",
"@angular/platform-browser": "^17.0.2",
"@angular/platform-browser-dynamic": "^17.0.2",
"@angular/router": "^17.0.2",
"@capacitor/app": "5.0.6",
"@capacitor/core": "5.5.1",
"@capacitor/haptics": "5.0.6",
"@capacitor/keyboard": "5.0.6",
"@capacitor/status-bar": "5.0.6",
"@ionic/angular": "^7.0.0",
"ionicons": "^7.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.0.0",
"@angular-eslint/builder": "^17.0.0",
"@angular-eslint/eslint-plugin": "^17.0.0",
"@angular-eslint/eslint-plugin-template": "^17.0.0",
"@angular-eslint/schematics": "^17.0.0",
"@angular-eslint/template-parser": "^17.0.0",
"@angular/cli": "^17.0.0",
"@angular/compiler-cli": "^17.0.2",
"@angular/language-service": "^17.0.2",
"@capacitor/cli": "5.5.1",
"@ionic/angular-toolkit": "^9.0.0",
"@types/jasmine": "~5.1.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^7.26.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsdoc": "30.7.6",
"eslint-plugin-prefer-arrow": "1.2.2",
"jasmine-core": "~5.1.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"ts-node": "^8.3.0",
"typescript": "~5.2.2"
},
"description": "An Ionic project"
}
我找到了解决该问题的方法。 在我的
storage.service.ts
的构造函数中,我添加了这一行:
if (!environment.production) connectStorageEmulator(this.storage, 'localhost', 9199);
现在可以使用模拟器了。不过,我更喜欢位于
app.module.ts
文件中的解决方案。
我已经尝试像这样提供存储空间,但也没有成功:
provideStorage(() => {
const storage = getStorage();
if (!environment.production) connectStorageEmulator(storage, 'localhost', 9199);
return storage;
})
如果有人能帮助我,我会很高兴:)
在您的
storage.service.ts
上,不要使用 getStorage
来检索存储实例,而是使用 Storage
类并注入到您的服务中。
存储服务应该是这样的。
import { Injectable } from '@angular/core';
import { getBlob, ref, Storage } from '@angular/fire/storage';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private storageReference: string = "my-reference";
// changes
constructor(private storage: Storage) {}
downloadImage(dirRef: string) {
return getBlob(ref(this.storage, `${this.storageReference}/${dirRef}/image1.jpg`))
.then(image => {
var urlCreator = window.URL || window.webkitURL;
return urlCreator.createObjectURL(image);
})
}
}
或者像这样
import { Injectable, inject } from '@angular/core';
import { getBlob, ref, Storage } from '@angular/fire/storage';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private storageReference: string = "my-reference";
// changes
private storage = inject(Storage);
downloadImage(dirRef: string) {
return getBlob(ref(this.storage, `${this.storageReference}/${dirRef}/image1.jpg`))
.then(image => {
var urlCreator = window.URL || window.webkitURL;
return urlCreator.createObjectURL(image);
})
}
}
这样它将使用
app.module.ts
文件中的配置。