简体   繁体   English

错误 NullInjectorError:R3InjectorError(AppModule)

[英]ERROR NullInjectorError: R3InjectorError(AppModule)

I am trying to save the value from my component to firebase using service.我正在尝试使用服务将组件中的值保存到 firebase。 But I am getting NullInjector Error when ever I use service.但是每当我使用服务时,我都会收到 NullInjector Error。 I did everything that was not the inte.net.我做了所有不是 inte.net 的事情。 Imported everything but it wont work.进口的一切,但它不会工作。 I already tried importing HttpClientModule, AngularFireDatabase but nothing works.我已经尝试导入 HttpClientModule、AngularFireDatabase,但没有任何效果。 I have tried to completely recreate the project by freshly installing everything.我试图通过全新安装所有内容来完全重新创建项目。 I have tried different versions of firebase and angular (currently firebase@7.24.0 @angular/fire@6.0.3).我尝试了不同版本的 firebase 和 angular(目前是 firebase@7.24.0 @angular/fire@6.0.3)。 Nothing works.什么都不管用。

This is my fireservice.service这是我的 fireservice.service

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Injectable({
  providedIn: 'root'
})
export class FireserviceService {

  constructor(private db: AngularFireDatabase) {
    
   }
   create(){
    return this.db.list('/shopping-carts').push({
      dateCreated:new Date().getTime()
    });
  }
}

this is my app module这是我的应用模块

import { FireserviceService } from './fireservice.service';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { AngularFirestore } from '@angular/fire/firestore';
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { BrainComponent } from './brain/brain.component';


@NgModule({
  declarations: [
    AppComponent,
    BrainComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule
  ],
  providers: [
    AngularFirestore,
    FireserviceService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { 
  constructor(){

  }
}

this is my compnent这是我的组件

import { FireserviceService } from './../fireservice.service';
import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'brain',
  templateUrl: './brain.component.html',
  styleUrls: ['./brain.component.css']
})
export class BrainComponent  {

  constructor(private cartService: FireserviceService) { }
  addToCart(val:any){
    console.log(val)
  }

}

This is the error I get这是我得到的错误

core.js:6142 ERROR NullInjectorError: R3InjectorError(AppModule)[FireserviceService -> AngularFireDatabase -> AngularFireDatabase -> AngularFireDatabase]: 
  NullInjectorError: No provider for AngularFireDatabase!
    at NullInjector.get (http://localhost:51081/vendor.js:38768:27)
    at R3Injector.get (http://localhost:51081/vendor.js:38935:33)
    at R3Injector.get (http://localhost:51081/vendor.js:38935:33)
    at R3Injector.get (http://localhost:51081/vendor.js:38935:33)
    at injectInjectorOnly (http://localhost:51081/vendor.js:32465:33)
    at Module.ɵɵinject (http://localhost:51081/vendor.js:32469:61)
    at Object.FireserviceService_Factory [as factory] (http://localhost:51081/main.js:280:159)
    at R3Injector.hydrate (http://localhost:51081/vendor.js:39103:35)
    at R3Injector.get (http://localhost:51081/vendor.js:38924:33)
    at NgModuleRef$1.get (http://localhost:51081/vendor.js:53004:33)

The site works fine until I add parameters to the constructor in the brain component.在我向 brain 组件中的构造函数添加参数之前,该站点工作正常。 But as soon as I add private car service: FireserviceService in the constructor in the brain component it won't even load HTML. I am new to angular and firebase. I am trying to save the value in the firebase database.但是一旦我在大脑组件的构造函数中添加私家车服务:FireserviceService它甚至不会加载HTML。我是angular和firebase的新手。我试图将值保存在firebase数据库中。 Please help.请帮忙。

You can see from the error message that the dependency container is missing a provider for AngularFireDatabase:您可以从错误消息中看到依赖容器缺少 AngularFireDatabase 的提供程序:

" No provider for AngularFireDatabase" “没有 AngularFireDatabase 的提供者”

If you use AngularFireDatabase you need to import that service in app.module.ts in providers - you're currently importing AngularFirestore.如果您使用 AngularFireDatabase,您需要在提供程序的 app.module.ts 中导入该服务 - 您当前正在导入 AngularFirestore。

  providers: [
    AngularFireDatabase,
    FireserviceService
  ],

Import HttpClientModule into app.module.ts and add HttpClientModule to imports array.HttpClientModule导入app.module.ts并将HttpClientModule添加到 imports 数组中。

import { HttpClientModule } from '@angular/common/http';

 imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],

Check in your service whether @Injectable annotation is as shown below:在您的服务中检查@Injectable注解是否如下所示:

@Injectable({
  providedIn: 'root'
})

This is because you are trying to use Angular Fire Database but imported Angular Firestore Module and Angular Firestore in app module.这是因为您尝试使用 Angular Fire 数据库,但在应用程序模块中导入了 Angular Firestore 模块和 Angular Firestore。

Replace AngularFirestoreModule with AngularFireDatabaseModule and AngularFirestore with AngularFireDatabase in app.module.ts在 app.module.ts 中将 AngularFirestoreModule 替换为 AngularFireDatabaseModule 并将 AngularFirestore 替换为 AngularFireDatabase

  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireDatabaseModule
  ],
  providers: [
    AngularFireDatabase,
    FireserviceService
  ],

However, I will recommend to use Angular Firestore instead of Angular Fire Database if you are starting new.但是,如果您是新手,我建议您使用 Angular Firestore 而不是 Angular Fire Database。

https://github.com/angular/angularfire/blob/master/docs/install-and-setup.md https://github.com/angular/angularfire/blob/master/docs/install-and-setup.md

This should work for you:这应该适合你:

In your app.module.ts > imports:[ ]在你的app.module.ts > imports:[ ]

Replace this:替换这个:

provideFirebaseApp(() => initializeApp(environment.firebase)),
provideFirestore(() => getFirestore())

with this:有了这个:

AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule

This thread may help ,这个线程可能会有所帮助

If you are using libraries and add firebase as dependency , you have to keep both place with the same version如果您正在使用库并添加firebase作为dependency项,则必须将两个位置保持相同的版本

Meaning if意思是如果

MainApp主应用

"dependencies": { 
  "@my/lib": "1.0.0",
  "firebase": "7.22.0" 
}

@my/lib @my/lib

"dependencies": { 
  "firebase": "7.24.0"  // <-- The version is higher
}

Then you will face the same error.然后你将面临同样的错误。 The way to fix this would be to bump the @another/lib version into MainApp from 7.22.0 to 7.24.0 .解决此问题的方法是将@another/lib版本从7.22.0 MainApp 7.24.0

See the linked issue to see why you shouldn't import it into the dependencies but into the peerDependencies请参阅链接问题以了解为什么不应将其导入dependencies项而应导入peerDependencies

I Has same issue.我有同样的问题。 I fixed this by changing from我通过改变来解决这个问题

import { Database, onValue, ref, set } from "firebase/database";

to

import { Database, onValue, ref, set } from "@angular/fire/database";

in service.在服务中。 (Angular 13 & Fire 7.3.0) (Angular 13 和 Fire 7.3.0)

you should check if yo have imported the HttpClientModule since services usually uses it and I didn't see it in your code.您应该检查您是否导入了HttpClientModule ,因为服务通常会使用它,而我在您的代码中没有看到它。 Try it and give me some feedback!尝试一下并给我一些反馈! I had this same problem and I did solve it by importing the HttpClientModule我有同样的问题,我确实通过导入HttpClientModule解决了它

I have imported it in app.module like this我已经像这样将它导入 app.module

 import { HttpClientModule } from '@angular/common/http'; // HERE @NgModule({ declarations: [ AppComponent ], imports: [... HttpClientModule // HERE... ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }

and also you should check if you have de @Injectable decorator with the providedIn: 'root' in the service class like this并且您还应该检查服务 class 中是否有带有providedIn: 'root'的 de @Injectable装饰器,如下所示

 @Injectable({ providedIn: 'root' // HERE }) export class SocialPostsService {

暂无
暂无

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

相关问题 错误:未捕获(承诺):NullInjectorError:R3InjectorError(AppModule)[baseURL] - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[baseURL] 错误 NullInjectorError:R3InjectorError(AppModule)[MessageService -&gt; MessageService -&gt; MessageService]: - ERROR NullInjectorError: R3InjectorError(AppModule)[MessageService -> MessageService -> MessageService]: 错误:未捕获(承诺):NullInjectorError:R3InjectorError(AppModule)Angular 13 - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule) Angular 13 NullInjectorError: R3InjectorError(AppModule) 添加提供程序后 - NullInjectorError: R3InjectorError(AppModule) After adding providers 未捕获(承诺):NullInjectorError: R3InjectorError(AppModule)[FormBuilder -&gt; FormBuilder -&gt; FormBuilder] - Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[FormBuilder -> FormBuilder -> FormBuilder] 错误错误:未捕获(承诺):NullInjectorError:R3InjectorError(AppModule)[NavbarComponent - &gt; NavbarComponent - ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[NavbarComponent -> NavbarComponent 错误 NullInjectorError: R3InjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS-&gt;[object Object]-&gt;TransferState-&gt; TransferState -&gt; TransferState] - ERROR NullInjectorError: R3InjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS->[object Object]->TransferState-> TransferState -> TransferState] NgxAuthFirebaseUIModule 显示 ERROR NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -&gt; ActivatedRoute -&gt; ActivatedRoute]: - NgxAuthFirebaseUIModule displaying ERROR NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: R3InjectorError(AppModule)[Router -&gt; Router -&gt; Router]: NullInjectorError: No provider for Router - NullInjectorError: R3InjectorError(AppModule)[Router -> Router -> Router]: NullInjectorError: No provider for Router 错误:未捕获(承诺中):NullInjectorError:R3InjectorError(GestionGeneralModule) - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(GestionGeneralModule)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM