1
1
# Include AngularFire
2
2
3
3
[ 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.
5
6
6
7
#### Create new project
7
8
@@ -22,7 +23,7 @@ $ npm install --save angularfire2 firebase
22
23
23
24
#### Get Firebase configuration details
24
25
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.
26
27
27
28
Firebase offers an easy way to get this, by showing a JavaScript object that you can copy and paste.
28
29
@@ -41,112 +42,50 @@ Firebase offers an easy way to get this, by showing a JavaScript object that you
41
42
};
42
43
```
43
44
44
- #### Create FirebaseModule
45
+ #### Configure the Environment
45
46
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.
47
49
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:
49
51
50
52
``` 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
+ }
64
61
};
65
-
66
- export const FirebaseModule = AngularFireModule .initializeApp (config , authConfig );
67
62
```
68
63
69
- #### Import FirebaseModule
64
+ To define the keys for production you need to update ` src/environments/environment.prod.ts ` .
70
65
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
72
67
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 ` .
75
69
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:
88
71
72
+ ``` typescript
73
+ import { AngularFireModule } from ' angularfire2' ;
74
+ import { environment } from ' ../environments/environment' ;
89
75
```
90
76
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 ` :
95
78
96
79
``` 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
104
88
})
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
-
130
89
```
131
90
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