Angular和Firebase Storage返回:TypeError:app.storage不是一个函数

niknxzdl  于 2023-10-22  发布在  Angular
关注(0)|答案(1)|浏览(279)

我试图将我的Angular应用与FireBase连接,但组件显示为空白,Chrome控制台显示"TypeError:app.storage不是一个功能“我做错了什么?先谢谢你了。”
ng --版本

Angular CLI: 13.3.11
Node: 16.15.0
Package Manager: npm 8.5.5
OS: win32 x64

Angular: 13.3.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.11
@angular-devkit/build-angular   13.3.11
@angular-devkit/core            13.3.11
@angular-devkit/schematics      13.3.11
@angular/cdk                    13.3.9
@angular/cli                    13.3.11
@angular/fire                   7.6.1
@angular/material               13.3.9
@schematics/angular             13.3.11
rxjs                            7.5.7
typescript                      4.6.4

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
import { AngularFirestoreModule } from '@angular/fire/compat/firestore';
import { AngularFireStorageModule } from '@angular/fire/compat/storage';
import { AngularFireDatabaseModule } from '@angular/fire/compat/database'

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { Page404Component } from './page404/page404.component';
import { environment } from 'src/environments/environment';
import { UploadComponent } from './upload/upload.component';

@NgModule({
  declarations: [
    AppComponent,
    Page404Component,
    UploadComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule,
    AngularFireStorageModule,
    AngularFirestoreModule,
    AngularFireDatabaseModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes, PreloadAllModules } from '@angular/router';
import { Page404Component } from './page404/page404.component';
import { AuthGuard } from '@auth/guards/auth.guard';
import { UploadComponent } from './upload/upload.component';

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./website/website.module').then(m => m.WebsiteModule)
  },
  {
    path: 'upload',
    component: UploadComponent
  },
  {
    path: 'cms',
    canActivate: [ AuthGuard ],
    loadChildren: () => import('./cms/cms.module').then(m => m.CmsModule)
  },
  {
    path: 'auth',
    loadChildren: () => import('./auth/auth.module').then(m => m.AuthModule)
  },
  {
    path: '**',
    component: Page404Component
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes,{
    preloadingStrategy: PreloadAllModules})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

......和测试组件......
upload.component.ts

import { Component, OnInit } from '@angular/core';

import { AngularFireStorage } from '@angular/fire/compat/storage';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.scss']
})
export class UploadComponent implements OnInit {

  constructor(
    private upload: AngularFireStorage
  ) { }

  ngOnInit(): void {
  }

}

upload.component.html

<p>upload works!</p>

Chrome控制台上的完整错误

Error: Uncaught (in promise): TypeError: app.storage is not a function
TypeError: app.storage is not a function
    at angular-fire-compat-storage.js:107:62
    at _ZoneDelegate.invoke (zone.js:372:1)
    at Zone.run (zone.js:134:1)
    at NgZone.runOutsideAngular (core.mjs:25593:1)
    at angular-fire-compat-storage.js:107:34
    at ɵcacheInstance (angular-fire-compat.js:147:29)
    at new AngularFireStorage (angular-fire-compat-storage.js:106:38)
    at Object.AngularFireStorage_Factory [as factory] (angular-fire-compat-storage.js:133:1)
    at R3Injector.hydrate (core.mjs:11579:1)
    at R3Injector.get (core.mjs:11398:1)
    at resolvePromise (zone.js:1211:1)
    at resolvePromise (zone.js:1165:1)
    at zone.js:1278:1
    at _ZoneDelegate.invokeTask (zone.js:406:1)
    at Object.onInvokeTask (core.mjs:25681:1)
    at _ZoneDelegate.invokeTask (zone.js:405:1)
    at Zone.runTask (zone.js:178:1)
    at drainMicroTaskQueue (zone.js:585:1)
    at ZoneTask.invokeTask [as invoke] (zone.js:491:1)
    at invokeTask (zone.js:1661:1)
yhived7q

yhived7q1#

我在初始化AngularFireStorage时遇到了这个错误。我通过将Firebase降级到版本9来修复它。当前版本10与@angular/fire 7.x不兼容。yet. see:https://github.com/angular/angularfire/tree/master#angular-and-firebase-versions
可能有点过头了,但是为了重置,我删除了我的nodes_modules目录,然后重新运行

npm install
npm i --save [email protected]
ng add @angular/fire

这就解决了我的项目。

相关问题