简体   繁体   English

收到“错误 TS2304:找不到名称‘firebase’。”

[英]Receiving "error TS2304: Cannot find name 'firebase'."

Trying to connect an AngularJS template to firebase, but cannot seem to solve the issue.尝试将 AngularJS 模板连接到 firebase,但似乎无法解决问题。

I have already npm installed everything I could including typings, angularfire, firebase, and a few more.我已经 npm 安装了所有我能安装的东西,包括 typings、angularfire、firebase 等等。

Here is my app.component.ts:这是我的 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);
  }

}

Here is my package.json:这是我的 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"
  },

The error I'm recieving in terminal: ERROR in src/app/app.component.ts(20,5): error TS2304: Cannot find name 'firebase'.我在终端收到的错误:src/app/app.component.ts(20,5) 中的错误:错误 TS2304:找不到名称“firebase”。

I have tried everything I have found on the web thus far and it seems that nothing is working.到目前为止,我已经尝试了在网上找到的所有内容,但似乎没有任何效果。 I'm assuming it is some simple fix, but I cannot find it.我假设这是一些简单的修复,但我找不到它。 Please let me know what other information/code you may need.请让我知道您可能需要的其他信息/代码。

To fix it you need to add 要修复它,您需要添加

import * as firebase from 'firebase';

BUT

You should use @angular/fire or ( angularfire2 for older versions) instead of firebase itself. 您应该使用@angular/fire或( angularfire2而不是对旧版本) firebase本身。

Then you can define the config in environment.ts and environment.prod.ts 然后,您可以在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"
        };
};

And then you have to initialize it inside of AppModule 然后你必须在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 {}

This will do the work instead firebase.initializeApp(config); 这将代替firebase.initializeApp(config); . To use other functionality you would also need to use other modules like AngularFireAuthModule and so on. 要使用其他功能,您还需要使用其他模块,如AngularFireAuthModule等。

What worked for me was importing firebaseapp对我有用的是导入 firebaseapp

import { FirebaseApp } from '@angular/fire/app';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用谷歌 api 库编译 typescript 时出现“错误 TS2304:找不到名称‘Long’” - "error TS2304: Cannot find name 'Long'" when compiling typescript with google api libraries Angular 和 TypeScript:找不到名称“Note”.ts(2304) - Angular and TypeScript: Cannot find name 'Note'.ts(2304) 找不到名称“持久性”。 您的意思是“setPersistence”吗? - Cannot find name 'Persistence'. Did you mean 'setPersistence'?ts FIrebase 部署错误:找不到模块“firebase-admin” - FIrebase deploy error: Cannot find module 'firebase-admin' 在 Angular 13 中的 firebase 集成期间找不到名称“PhoneOrOauthTokenResponse” - Cannot find name 'PhoneOrOauthTokenResponse' during firebase integration in Angular 13 Vue3.js )错误:找不到模块'firebase' - Vue3.js ) Error: Cannot find module 'firebase' 部署 firebase 项目时出现错误显示“找不到模块 firebase-functions” - Error shows 'cannot find module firebase-functions' when deploying a firebase project 错误:运行时出现“找不到模块” firebase 云 function - Error: "Cannot find module" while running firebase cloud function 初始化“@nuxtjs/firebase”模块时出现 Nuxt 致命错误 - 找不到模块“firebase/compat/app” - Nuxt FATAL error when initializing '@nuxtjs/firebase' module - Cannot find module 'firebase/compat/app' 未捕获的错误:尝试在 firestore 中使用 serverTimestamp 时找不到模块“firebase” - Uncaught Error: Cannot find module 'firebase' when trying to use serverTimestamp in firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM