【问题标题】:Receiving "error TS2304: Cannot find name 'firebase'."收到“错误 TS2304:找不到名称‘firebase’。”
【发布时间】:2019-08-17 01:15:13
【问题描述】:

尝试将 AngularJS 模板连接到 firebase,但似乎无法解决问题。

我已经 npm 安装了所有我能安装的东西,包括打字、angularfire、firebase 等等。

这是我的 app.component.ts:

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

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'app';

  ngOnInit() {
    const config = {
      apiKey: "AIzaSyBKOJjrzrgT0rXZxonOgk8qM-OIlUjJA4I",
      authDomain: "tn-test-0001.firebaseapp.com",
      databaseURL: "https://tn-test-0001.firebaseio.com",
      projectId: "tn-test-0001",
      storageBucket: "tn-test-0001.appspot.com",
      messagingSenderId: "564114143143"
    };
    firebase.initializeApp(config);
  }

}

这是我的 package.json:

  "private": true,
  "dependencies": {
    '''
    "angular-tree-component": "^7.0.2",
    "angular2-text-mask": "^8.0.4",
    "angularfire2": "5.0.0-rc.4",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.4.1",
    "d3": "^3.5.17",
    "echarts": "^4.0.4",
    "firebase": "^4.3.0",
    "hammerjs": "^2.0.8",
    "https": "^1.0.0",
    "imagesloaded": "^4.1.4",
    "isotope-layout": "^3.0.6",
    "mandrill-api": "^1.0.45",
    "masonry-layout": "^4.2.1",
    "moment": "^2.18.1",
    "ng-scrollreveal": "^2.2.0",
    "ngx-bootstrap": "^2.0.5",
    "ngx-echarts": "^3.1.0",
    "ngx-filter-pipe": "^2.1.0",
    "ngx-isotope": "^0.1.7",
    "ngx-nvd3": "^1.0.9",
    "ngx-perfect-scrollbar": "^5.3.5",
    "ngx-quill": "^2.1.2",
    "ngx-swiper-wrapper": "^6.0.0",
    "rickshaw": "^1.6.5",
    "rxjs": "^6.0.0",
    "rxjs-compat": "^6.4.0",
    "scrollreveal": "^3.3.6",
    "text-mask-addons": "^3.7.1",
    "web-animations-js": "^2.3.1",
    "zone.js": "0.8.26"
  },

我在终端收到的错误: src/app/app.component.ts(20,5) 中的错误:错误 TS2304:找不到名称“firebase”。

到目前为止,我已经尝试了我在网络上找到的所有内容,但似乎没有任何效果。我假设这是一些简单的修复,但我找不到它。请让我知道您可能需要哪些其他信息/代码。

【问题讨论】:

  • 你在哪里导入了 firebase 模块?
  • 您没有导入 firebase。这也是 Angular7,而不是 AngularJS。
  • 如果您使用@angular/fire(或angularfire2)库,您应该使用AngularFireModule.initializeApp() 添加和初始化Firebase 应用,并将您的Firebase 应用配置作为参数传递。请参阅github.com/angular/angularfire2/blob/master/docs/… 了解更多信息
  • 不要在 Stack Overflow 上发布您的 API 密钥。

标签: angular firebase angularfire2


【解决方案1】:

要修复它,您需要添加

import * as firebase from 'firebase';

但是

您应该使用@angular/fire 或(旧版本为angularfire2)而不是firebase 本身。

然后你可以在environment.tsenvironment.prod.ts中定义配置

export const environment = {
  production: false;
  config = {
          apiKey: "AIzaSyBKOJjrzrgT0rXZxonOgk8qM-OIlUjJA4I",
          authDomain: "tn-test-0001.firebaseapp.com",
          databaseURL: "https://tn-test-0001.firebaseio.com",
          projectId: "tn-test-0001",
          storageBucket: "tn-test-0001.appspot.com",
          messagingSenderId: "564114143143"
        };
};

然后你必须在AppModule内部初始化它

...
import { AngularFireModule } from '@angular/fire';
import { environment } from './environment';

@NgModule({
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.config)
    // Other modules
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}

这将代替 firebase.initializeApp(config); 完成工作。要使用其他功能,您还需要使用其他模块,例如 AngularFireAuthModule 等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-30
    • 2016-12-18
    • 2019-05-06
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多