|
| 1 | +# Include AngularFire |
| 2 | + |
| 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. |
| 5 | + |
| 6 | +#### Create new project |
| 7 | + |
| 8 | +Create a new project and navigate into the project. |
| 9 | + |
| 10 | +```bash |
| 11 | +$ ng new my-app |
| 12 | +$ cd my-app |
| 13 | +``` |
| 14 | + |
| 15 | +#### Install dependencies |
| 16 | + |
| 17 | +In the new project you need to install the required dependencies. |
| 18 | + |
| 19 | +```bash |
| 20 | +$ npm install --save angularfire2 firebase |
| 21 | +``` |
| 22 | + |
| 23 | +#### Get Firebase configuration details |
| 24 | + |
| 25 | +In order to connect AngularFire to Firebase we need to get the configuration details. |
| 26 | + |
| 27 | +Firebase offers an easy way to get this, by showing a JavaScript object that you can copy and paste. |
| 28 | + |
| 29 | +- Log in to the [Firebase](https://firebase.google.com) console. |
| 30 | +- Create New Project or open an existing one. |
| 31 | +- Click `Add Firebase to your web app`. |
| 32 | +- From the modal window that pops up you copy the `config` object. |
| 33 | + |
| 34 | +```javascript |
| 35 | + var config = { |
| 36 | + apiKey: "your-api-key", |
| 37 | + authDomain: "your-auth-domain", |
| 38 | + databaseURL: "your-database-url", |
| 39 | + storageBucket: "your-storage-bucket", |
| 40 | + messagingSenderId: "your-message-sender-id" |
| 41 | + }; |
| 42 | +``` |
| 43 | + |
| 44 | +#### Create FirebaseModule |
| 45 | + |
| 46 | +We need a way to store these configuration details in our app. We do this by creating a module. |
| 47 | + |
| 48 | +Create a new file `src/app/firebase.ts` with the content below and pass in the variables you retrieved from Firebase. |
| 49 | + |
| 50 | +```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, |
| 64 | +}; |
| 65 | + |
| 66 | +export const FirebaseModule = AngularFireModule.initializeApp(config, authConfig); |
| 67 | +``` |
| 68 | + |
| 69 | +#### Import FirebaseModule |
| 70 | + |
| 71 | +In `src/app/app.module.ts` we need to reference the FirebaseModule so it gets imported in our app. |
| 72 | + |
| 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. |
| 75 | + |
| 76 | +```typescript |
| 77 | +/* other imports */ |
| 78 | +import { FirebaseModule } from './firebase'; |
| 79 | + |
| 80 | +@NgModule({ |
| 81 | + ... |
| 82 | + imports: [ |
| 83 | + ... |
| 84 | + FirebaseModule |
| 85 | + ] |
| 86 | + ... |
| 87 | +}) |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +# W.I.P. |
| 93 | + |
| 94 | +#### Use Firebase in your components |
| 95 | + |
| 96 | +```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'] |
| 104 | +}) |
| 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 | +``` |
| 131 | + |
| 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 | +``` |
0 commit comments