What is @angular/fire?
@angular/fire is the official Angular library for Firebase. It provides a set of AngularFire modules that enable Angular applications to interact with Firebase services such as Firestore, Authentication, Storage, and more. This library simplifies the integration of Firebase with Angular applications, offering reactive and declarative APIs.
What are @angular/fire's main functionalities?
Firestore
This feature allows you to interact with Firestore, a NoSQL database provided by Firebase. The code sample demonstrates how to retrieve a collection of items from Firestore and subscribe to changes in real-time.
import { AngularFirestore } from '@angular/fire/compat/firestore';
constructor(private firestore: AngularFirestore) {}
getItems() {
return this.firestore.collection('items').valueChanges();
}
Authentication
This feature enables authentication functionalities such as signing in, signing out, and managing user sessions. The code sample shows how to sign in a user using email and password.
import { AngularFireAuth } from '@angular/fire/compat/auth';
constructor(private afAuth: AngularFireAuth) {}
login(email: string, password: string) {
return this.afAuth.signInWithEmailAndPassword(email, password);
}
Storage
This feature allows you to interact with Firebase Storage, which is used for storing and serving user-generated content such as images and videos. The code sample demonstrates how to upload a file to Firebase Storage.
import { AngularFireStorage } from '@angular/fire/compat/storage';
constructor(private storage: AngularFireStorage) {}
uploadFile(filePath: string, file: File) {
const fileRef = this.storage.ref(filePath);
return fileRef.put(file);
}
Realtime Database
This feature provides access to Firebase Realtime Database, a cloud-hosted NoSQL database that allows data to be stored and synchronized in real-time. The code sample shows how to retrieve a list of items from the Realtime Database.
import { AngularFireDatabase } from '@angular/fire/compat/database';
constructor(private db: AngularFireDatabase) {}
getItems() {
return this.db.list('items').valueChanges();
}
Analytics
This feature enables the use of Firebase Analytics to log events and track user interactions within your application. The code sample demonstrates how to log a custom event.
import { AngularFireAnalytics } from '@angular/fire/compat/analytics';
constructor(private analytics: AngularFireAnalytics) {}
logEvent(eventName: string, eventParams: any) {
this.analytics.logEvent(eventName, eventParams);
}
Other packages similar to @angular/fire
firebase
The 'firebase' package is the official Firebase JavaScript SDK. It provides comprehensive access to Firebase services but lacks the Angular-specific integrations and reactive APIs provided by @angular/fire. Developers using Angular would need to manually integrate Firebase services and manage state updates.
reactfire
The 'reactfire' package is the official Firebase library for React. It offers similar functionalities to @angular/fire but is tailored for React applications. It provides hooks and components to easily integrate Firebase services with React's component-based architecture.
vuefire
The 'vuefire' package is the official Firebase library for Vue.js. It provides bindings to integrate Firebase services with Vue.js applications, similar to how @angular/fire integrates with Angular. It offers Vue-specific features such as directives and plugins.
AngularFire
AngularFire smooths over the rough edges an Angular developer might encounter when implementing the framework-agnostic
Firebase JS SDK & aims to provide a more natural developer experience
by conforming to Angular conventions.
ng add @angular/fire
- Dependency injection - Provide and Inject Firebase services in your components.
- Zone.js wrappers - Stable zones allow proper functionality of service workers, forms, SSR, and pre-rendering.
- Observable based - Utilize RxJS rather than callbacks for real-time streams.
- NgRx friendly API - Integrate with NgRx using AngularFire's action based APIs.
- Lazy-loading - AngularFire dynamically imports much of Firebase, reducing the time to load your app.
- Deploy schematics - Get your Angular application deployed on Firebase Hosting with a single command.
- Google Analytics - Zero-effort Angular Router awareness in Google Analytics.
- Router Guards - Guard your Angular routes with built-in Firebase Authentication checks.
Example use
import { provideFirebaseApp, getApp, initializeApp } from '@angular/fire/app';
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
@NgModule({
imports: [
provideFirebaseApp(() => initializeApp({ ... })),
provideFirestore(() => getFirestore()),
],
...
})
export class AppModule { }
import { inject } from '@angular/core';
import { Firestore, collectionData, collection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
interface Item {
name: string,
...
};
@Component({
selector: 'app-root',
template: `
<ul>
<li *ngFor="let item of item$ | async">
{{ item.name }}
</li>
</ul>
`
})
export class AppComponent {
item$: Observable<Item[]>;
firestore: Firestore = inject(Firestore);
constructor() {
const itemCollection = collection(this.firestore, 'items');
this.item$ = collectionData<Item>(itemCollection);
}
}
Polyfills
Neither AngularFire nor Firebase ship with polyfills. To have compatibility across a wide-range of environments, we suggest the following polyfills be added to your application:
Resources
Quickstart - Get your first application up and running by following our quickstart guide.
Contributing
Stackblitz Template - Remember to set your Firebase configuration in app/app.module.ts
.
Upgrading to v7.0? Check out our guide.
Sample apps
We have three sample apps in this repository:
samples/compat
a kitchen sink application that demonstrates use of the "compatibility" APIsamples/modular
a kitchen sink application that demonstrates the new tree-shakable APIsamples/advanced
the same app as samples/modular
but demonstrates more advanced concepts such as Angular Universal state-transfer, dynamically importing Firebase feature modules, and Firestore data bundling.
Having troubles?
Get help on our Q&A board, the official Firebase Mailing List, the Firebase Community Slack (#angularfire2
), the Angular Community Discord (#firebase
), Gitter, the Firebase subreddit, or Stack Overflow.
NOTE: AngularFire is maintained by Googlers but is not a supported Firebase product. Questions on the mailing list and issues filed here are answered on a best-effort basis by maintainers and other community members. If you are able to reproduce a problem with Firebase outside of AngularFire's implementation, please file an issue on the Firebase JS SDK or reach out to the personalized Firebase support channel.
Developer Guide
This developer guide assumes you're using the new tree-shakable AngularFire API, if you're looking for the compatability API you can find the documentation here.
See the v7 upgrade guide for more information on this change..
Firebase product integrations
import { } from '@angular/fire/analytics';
|
import { } from '@angular/fire/auth';
|
import { } from '@angular/fire/firestore';
|
import { } from '@angular/fire/functions';
|
import { } from '@angular/fire/messaging';
|
import { } from '@angular/fire/storage';
|
import { } from '@angular/fire/performance';
|
import { } from '@angular/fire/database';
|
import { } from '@angular/fire/remote-config';
|
import { } from '@angular/fire/app-check';
|
Deploying your site
- Deploy to Firebase Hosting
- Angular Universal: Deploy to Cloud Functions
- Angular Universal: Deploy to Cloud Run