属性 'firebase' 类型不存在 { production: boolean; }

Property 'firebase' does not exist on type { production: boolean; }

所以我试图在 Firebase 和 Heroku 上构建和部署我的 Angular 4 生产应用程序,但我遇到了如下错误:

ERROR in /Users/.../... (57,49): Property 'firebase' does not exist on type '{ production: boolean; }'.

它发生在我 运行 ng build --prod 并且我的部署服务器运行良好时。这是我的app.module.ts文件,供参考:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { Ng2ScrollimateModule } from 'ng2-scrollimate';
import { Ng2PageScrollModule } from 'ng2-page-scroll';

import { HttpModule } from '@angular/http';
import { trigger, state, style, animate, transition, keyframes } from '@angular/animations';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { environment } from '../environments/environment';

import { AppComponent } from './app.component';

import { LogoComponent } from './logo/logo.component';
import { InfoComponent } from './info/info.component';
import { AboutComponent } from './about/about.component';
import { DividerComponent } from './divider/divider.component';
import { ProficienciesComponent } from './proficiencies/proficiencies.component';
import { ProficiencyComponent } from './proficiency/proficiency.component';
import { PortfolioComponent } from './portfolio/portfolio.component';
import { ProjectComponent } from './project/project.component';
import { ResumeComponent } from './resume/resume.component';
import { FooterComponent } from './footer/footer.component';
import { ContactComponent } from './contact/contact.component';
import { LoadingComponent } from './loading/loading.component';

@NgModule({
  declarations: [
    AppComponent,
    LogoComponent,
    InfoComponent,
    AboutComponent,
    DividerComponent,
    ProficienciesComponent,
    ProficiencyComponent,
    PortfolioComponent,
    ProjectComponent,
    ResumeComponent,
    FooterComponent,
    ContactComponent,
    LoadingComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    BrowserAnimationsModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireDatabaseModule,
    Ng2ScrollimateModule,
    Ng2PageScrollModule.forRoot(),
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

environment.prod.ts

export const environment = {
  production: true
};

environment.ts

export const environment = {
  production: true,
  firebase: {
    apiKey: '...',
    authDomain: 'project.firebaseapp.com',
    databaseURL: 'https://project.firebaseio.com',
    projectId: 'project',
    storageBucket: 'project.appspot.com',
    messagingSenderId: '...',
  },
};

在搜索 Whosebug 和 GitHub 寻找可能的解决方案后,似乎没有开发人员遇到过这个确切的错误并发布了他们的发现,所以我想知道是否有人知道如何解决这个问题。提前致谢!

当您 运行 ng build --prod angular-cli 将使用 environment.prod.ts 文件而您的 environment.prod.ts 文件 environment 变量不会有 firebase 字段,因此你得到了异常。

将字段添加到 environment.prod.ts

export const environment = {
  production: true,
  firebase: {
    apiKey: '...',
    authDomain: 'project.firebaseapp.com',
    databaseURL: 'https://project.firebaseio.com',
    projectId: 'project',
    storageBucket: 'project.appspot.com',
    messagingSenderId: '...',
  },
};

确保 firebase: 之间没有间隙。

即应该是firebase:,而不是firebase :

我讨厌重复的代码
所以让我们创建一个名为 environments/firebase.ts 的单独文件,内容为

export const firebase = {
    //...
};

用法

import {firebase} from '../environments/firebase';
//...
AngularFireModule.initializeApp(firebase)

我都清楚

我的方法是将公共环境对象与产品对象合并。这是我的 environment.prod.ts:

import { environment as common } from './environment';

export const environment = {
  ...common,
  production: true
};

所以公共环境对象充当所有其他环境的可覆盖默认值。

我遇到了同样的错误,因为我将 'firebase' 拼错为 'firebas'

firebas: { api密钥:“...”, 授权域:"project.firebaseapp.com", 数据库网址:“https://project.firebaseio.com”, 项目编号:"project", 存储桶:"project.appspot.com", messagingSenderId: "..." }