Skip to content

Commit 22424f2

Browse files
committed
chore(docs): update documentation to integrate AngularFire
1 parent 966bb8f commit 22424f2

File tree

1 file changed

+32
-93
lines changed

1 file changed

+32
-93
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Include AngularFire
22

33
[Firebase](https://firebase.google.com/) is a mobile and web application platform with tools and infrastructure designed
4-
to help developers build high-quality apps.
4+
to help developers build high-quality apps. [AngularFire2](https://github.com/angular/angularfire2) is the official
5+
Angular library to use Firebase in your apps.
56

67
#### Create new project
78

@@ -22,7 +23,7 @@ $ npm install --save angularfire2 firebase
2223

2324
#### Get Firebase configuration details
2425

25-
In order to connect AngularFire to Firebase we need to get the configuration details.
26+
In order to connect AngularFire to Firebase you need to get the configuration details.
2627

2728
Firebase offers an easy way to get this, by showing a JavaScript object that you can copy and paste.
2829

@@ -41,112 +42,50 @@ Firebase offers an easy way to get this, by showing a JavaScript object that you
4142
};
4243
```
4344

44-
#### Create FirebaseModule
45+
#### Configure the Environment
4546

46-
We need a way to store these configuration details in our app. We do this by creating a module.
47+
These configuration details need to be stored in our app, one way to do this using the `environment`. This allows you to
48+
use different credentials in development and production.
4749

48-
Create a new file `src/app/firebase.ts` with the content below and pass in the variables you retrieved from Firebase.
50+
Open `src/environments/environment.ts` and add a key `firebase` to the exported constant:
4951

5052
```typescript
51-
import { AngularFireModule, AuthMethods } from 'angularfire2';
52-
import { FirebaseAppConfig } from 'angularfire2/interfaces';
53-
import { AuthConfiguration } from 'angularfire2/auth';
54-
55-
const config: FirebaseAppConfig = {
56-
apiKey: 'your-api-key',
57-
authDomain: 'your-auth-domain',
58-
databaseURL: 'your-database-url',
59-
storageBucket: 'your-storage-bucket',
60-
};
61-
62-
const authConfig: AuthConfiguration = {
63-
method: AuthMethods.Popup,
53+
export const environment = {
54+
production: false,
55+
firebase: {
56+
apiKey: 'your-api-key',
57+
authDomain: 'your-auth-domain',
58+
databaseURL: 'your-database-url',
59+
storageBucket: 'your-storage-bucket',
60+
}
6461
};
65-
66-
export const FirebaseModule = AngularFireModule.initializeApp(config, authConfig);
6762
```
6863

69-
#### Import FirebaseModule
64+
To define the keys for production you need to update `src/environments/environment.prod.ts`.
7065

71-
In `src/app/app.module.ts` we need to reference the FirebaseModule so it gets imported in our app.
66+
#### Import and load FirebaseModule
7267

73-
On the top of the file you import the module from the file created in the previous step. In the array of imports you
74-
reference the module.
68+
The final step is to import `AngularFireModule` and initialize it using the parameters from the `environment`.
7569

76-
```typescript
77-
/* other imports */
78-
import { FirebaseModule } from './firebase';
79-
80-
@NgModule({
81-
...
82-
imports: [
83-
...
84-
FirebaseModule
85-
]
86-
...
87-
})
70+
Open `src/app/app.module.ts` and add the following lines on the top of the file, with the other imports:
8871

72+
```typescript
73+
import { AngularFireModule } from 'angularfire2';
74+
import { environment } from '../environments/environment';
8975
```
9076

91-
92-
# W.I.P.
93-
94-
#### Use Firebase in your components
77+
To initialize AngularFire add the following line to the `imports` array inside the `NgModule`:
9578

9679
```typescript
97-
import { Component } from '@angular/core';
98-
import { AngularFire, FirebaseListObservable } from 'angularfire2';
99-
100-
@Component({
101-
selector: 'app-root',
102-
templateUrl: './app.component.html',
103-
styleUrls: ['./app.component.css']
80+
@NgModule({
81+
// declarations
82+
imports: [
83+
// BrowserModule, etc
84+
AngularFireModule.initializeApp(environment.firebase),
85+
]
86+
// providers
87+
// bootstrap
10488
})
105-
export class AppComponent {
106-
title = 'Firebase TODO';
107-
108-
public todos: FirebaseListObservable<any[]>;
109-
110-
constructor(
111-
private af: AngularFire
112-
) {
113-
this.todos = af.database.list('/todos');
114-
}
115-
116-
addTodo(name) {
117-
this.todos.push({ name: name.value, done: false });
118-
name.value = null;
119-
}
120-
121-
toggleDone(todo) {
122-
this.todos.update(todo, { done: !todo.done });
123-
}
124-
125-
removeTodo(todo) {
126-
this.todos.remove(todo);
127-
}
128-
}
129-
13089
```
13190

132-
133-
```html
134-
<h1>
135-
{{title}}
136-
</h1>
137-
138-
<form (submit)="addTodo(name)">
139-
<input type="text" #name placeholder="Name">
140-
<button type="submit">Add</button>
141-
</form>
142-
143-
<ul>
144-
<li *ngFor="let todo of todos | async">
145-
<span [ngStyle]="{'text-decoration': todo.done ? 'line-through' : ''}">
146-
{{todo.name}}
147-
</span>
148-
<a href="#" (click)="removeTodo(todo)">Delete</a>
149-
<a href="#" (click)="toggleDone(todo)">Done</a>
150-
</li>
151-
</ul>
152-
```
91+
#### Congratulations, you can now use Firebase in your Angular app!

0 commit comments

Comments
 (0)