属性“ firebase”在类型{production:boolean; }


80

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

/ Users / ... / ...(57,49)中的错误:类型'{不存在属性'firebase'{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 {}

环境产品

export const environment = {
  production: true
};

环境

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

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


您的environment导入看起来像什么?
eko

@echonax让我用我的代码为您更新问题。
Anthony Krivonos

Answers:


197

当您运行ng build --prodangular-cli时,将使用该environment.prod.ts文件,而您的environment.prod.tsfilesenvironment变量没有该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: '...',
  },
};

12
先生,您是救生员。我要做的就是将environment.ts代码复制到environment.prod.ts文件中。然后,我简单地运行:ng build --prodfirebase deploy
Anthony Krivonos

1
@AnthonyKrivonos很高兴我能帮助:-)
EKO

我同意,现在我得到的Firebase不属于....,而我在两个环境中都添加了它
user7082181 '19

6

我的方法是将通用环境对象与生产对象合并。这是我的environment.prod.ts

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

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

因此,公共环境对象是所有其他环境的可替代默认值。


这种方法对我来说看起来不错,但不确定Angular 7 cli为什么显示警告,**警告已检测到循环依赖项:src \ environments \ environment.ts-> src \ environments \ environment.ts **
user2243747

3

我讨厌代码中的重复项,
因此让我们创建一个单独的文件environments/firebase.ts,内容为content

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

用法

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

就我而言一切都清楚


那么这将如何克服以下问题:当调用ng build --prod时,它将包括该文件;当调用ng serve时,它将调用另一个文件?
eko

@echonax是的,我看到它在main.<hash>.js文件
弗拉德

我不知道那意味着什么,但让我这样说。当OP电话纳克服务,角CLI会寻找environment.ts,当ng build --prod被调用时,角CLI将寻找environment.prod.ts。通过像您的答案一样操作,产品和开发环境将使用相同的配置。
eko

1

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

firebas:{apiKey:“ ...”,authDomain:“ project.firebaseapp.com”,databaseURL:“ https://project.firebaseio.com ”,projectId:“ project”,storageBucket:“ project.appspot.com” ,messagesSenderId:“ ...”}


-12

确保firebase和之间没有间隙:

也就是说,应该是firebase:,不是firebase :


我看到有些人否决了这个答案。但是,就我而言,此答案解决了我面临的问题。谢谢!
安妮
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.