如何修复空白页NullInjectorError

时间:2019-09-29 20:35:13

标签: angular angularfire

enter code here之后,放入“从'@ angular / fire / database'导入{AngularFireDatabase};'我的页面空白了,但是在Visual Studio中没有错误,因为可以通过ng serve进行编译,但是当我在Google控制台上检查时出现NullInjectorError

这是我的代码

user.service.ts

import { Injectable } from '@angular/core';
 import {AngularFireDatabase} from '@angular/fire/database';
import * as firebase from 'firebase';


@Injectable({
  providedIn: 'root'
})

export class UserService {

  constructor(private db: AngularFireDatabase) { }


save(user: firebase.User) {
this.db.object('/users/' + user.uid).update({
   name: user.displayName,
   email: user.email
 });
}

}

1 个答案:

答案 0 :(得分:1)

您需要导入模块并在app.module.ts中初始化它

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase, 'my-app-name'), // imports firebase/app needed for everything
    AngularFireDatabaseModule
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

请检查其Installation and Setup自述文件。