npm install -g @angular/cli
ng new <project-name>
cd <project-name>
The Angular CLI's new
command will set up the latest Angular build in a new project structure.
ng add @angular/fire
Now that you have a new project setup, install AngularFire and Firebase from npm.
Open /src/environments/environment.ts
and add your Firebase configuration. You can find your project configuration in the Firebase Console. Click the Gear icon next to Project Overview, then click Project Settings and under "Firebase SDK snippet" click Config.
export const environment = {
production: false,
firebase: {
apiKey: '<your-key>',
authDomain: '<your-project-authdomain>',
databaseURL: '<your-database-URL>',
projectId: '<your-project-id>',
storageBucket: '<your-storage-bucket>',
messagingSenderId: '<your-messaging-sender-id>'
}
};
Open /src/app/app.module.ts
, inject the Firebase providers, and specify your Firebase configuration.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebase)
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
After adding the AngularFireModule you also need to add modules for the individual @NgModules that your application needs.
For example if your application was using both Google Analytics and the Firestore you would add AngularFireAnalyticsModule
and AngularFirestoreModule
:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire';
import { AngularFireAnalyticsModule } from '@angular/fire/analytics';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { environment } from '../environments/environment';
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireAnalyticsModule,
AngularFirestoreModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
Open /src/app/app.component.ts
, and make sure to modify/delete any tests to get the sample working (tests are still important, you know):
import { Component } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
constructor(firestore: AngularFirestore) {
}
}
In /src/app/app.component.ts
:
import { Component } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
this.items = firestore.collection('items').valueChanges();
}
}
Open /src/app/app.component.html
:
<ul>
<li class="text" *ngFor="let item of items | async">
{{item.name}}
</li>
</ul>
ng serve
Your Angular app will compile and serve locally, visit it we should see an empty list.
In another tab start adding data to an items
collection in Firestore. As we're not authenticating users yet, be sure to start Firestore in test mode or allow reading from the items
collection in Security Rules (allow read: if true
).
Once you've created a items
collection and are inserting documents, you should see data streaming into your Angular application.
Finally, we can deploy the application to Firebase hosting:
ng deploy