|
| 1 | +# 1. Installation and Setup |
| 2 | + |
| 3 | +> Using Ionic and the Ionic CLI? Check out these [specific instructions](ionic/cli.md) for Ionic and their CLI. |
| 4 | +
|
| 5 | +### 0. Prerequisites |
| 6 | + |
| 7 | +AngularFire provides multiple module formats for different types of builds. The guide is based off the Angular CLI. It is possible to do a manual setup with Webpack or a SystemJS build as well. |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @angular/cli |
| 11 | +``` |
| 12 | + |
| 13 | +### 1. Create a new project |
| 14 | + |
| 15 | +```bash |
| 16 | +ng new <project-name> |
| 17 | +cd <project-name> |
| 18 | +``` |
| 19 | + |
| 20 | +The Angular CLI's `new` command will set up the latest Angular build in a new project structure. |
| 21 | + |
| 22 | +### 2. Test your setup |
| 23 | + |
| 24 | +```bash |
| 25 | +ng serve |
| 26 | +open http://localhost:4200 |
| 27 | +``` |
| 28 | + |
| 29 | +You should see a message on the page that says *App works!* |
| 30 | + |
| 31 | +### 3. Install AngularFire and Firebase |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install angularfire2 firebase --save |
| 35 | +``` |
| 36 | + |
| 37 | +Now that you have a new project setup, install AngularFire and Firebase from npm. |
| 38 | + |
| 39 | +### 4. Add Firebase config to environments variable |
| 40 | + |
| 41 | +Open `/src/environments/environment.ts` and add your Firebase configuration. You can find your project configuration in [the Firebase Console](https://console.firebase.google.com). From the project overview page, click **Add Firebase to your web app**. |
| 42 | + |
| 43 | +```ts |
| 44 | +export const environment = { |
| 45 | + production: false, |
| 46 | + firebase: { |
| 47 | + apiKey: '<your-key>', |
| 48 | + authDomain: '<your-project-authdomain>', |
| 49 | + databaseURL: '<your-database-URL>', |
| 50 | + projectId: '<your-project-id>', |
| 51 | + storageBucket: '<your-storage-bucket>', |
| 52 | + messagingSenderId: '<your-messaging-sender-id>' |
| 53 | + } |
| 54 | +}; |
| 55 | +``` |
| 56 | + |
| 57 | +### 5. Setup @NgModule for the AngularFireModule |
| 58 | + |
| 59 | +Open `/src/app/app.module.ts`, inject the Firebase providers, and specify your Firebase configuration. |
| 60 | + |
| 61 | +```ts |
| 62 | +import { BrowserModule } from '@angular/platform-browser'; |
| 63 | +import { NgModule } from '@angular/core'; |
| 64 | +import { AppComponent } from './app.component'; |
| 65 | +import { AngularFireModule } from 'angularfire2'; |
| 66 | +import { environment } from '../environments/environment'; |
| 67 | + |
| 68 | +@NgModule({ |
| 69 | + imports: [ |
| 70 | + BrowserModule, |
| 71 | + AngularFireModule.initializeApp(environment.firebase) |
| 72 | + ], |
| 73 | + declarations: [ AppComponent ], |
| 74 | + bootstrap: [ AppComponent ] |
| 75 | +}) |
| 76 | +export class AppModule {} |
| 77 | +``` |
| 78 | + |
| 79 | +#### Custom `FirebaseApp` names |
| 80 | + |
| 81 | +You can optionally provide a custom FirebaseApp name with `initializeApp`. |
| 82 | + |
| 83 | +```ts |
| 84 | +@NgModule({ |
| 85 | + imports: [ |
| 86 | + BrowserModule, |
| 87 | + AngularFireModule.initializeApp(environment.firebase, 'my-app-name') |
| 88 | + ], |
| 89 | + declarations: [ AppComponent ], |
| 90 | + bootstrap: [ AppComponent ] |
| 91 | +}) |
| 92 | +export class AppModule {} |
| 93 | +``` |
| 94 | + |
| 95 | +### 6. Setup individual `@NgModules` |
| 96 | + |
| 97 | +After adding the AngularFireModule you also need to add modules for the individual @NgModules that your application needs. |
| 98 | + |
| 99 | + - `AngularFirestoreModule` |
| 100 | + - `AngularFireAuthModule` |
| 101 | + - `AngularFireDatabaseModule` |
| 102 | + - `AngularFireStorageModule` |
| 103 | + - `AngularFireMessagingModule` (Future release) |
| 104 | + |
| 105 | +#### Adding the Firebase Database and Auth Modules |
| 106 | + |
| 107 | +For example if your application was using both Firebase authentication and the Firebase database you would add: |
| 108 | + |
| 109 | +```ts |
| 110 | +import { BrowserModule } from '@angular/platform-browser'; |
| 111 | +import { NgModule } from '@angular/core'; |
| 112 | +import { AppComponent } from './app.component'; |
| 113 | +import { AngularFireModule } from 'angularfire2'; |
| 114 | +import { AngularFirestoreModule } from 'angularfire2/firestore'; |
| 115 | +import { AngularFireStorageModule } from 'angularfire2/storage'; |
| 116 | +import { AngularFireAuthModule } from 'angularfire2/auth'; |
| 117 | +import { environment } from '../environments/environment'; |
| 118 | + |
| 119 | +@NgModule({ |
| 120 | + imports: [ |
| 121 | + BrowserModule, |
| 122 | + AngularFireModule.initializeApp(environment.firebase, 'my-app-name'), // imports firebase/app needed for everything |
| 123 | + AngularFirestoreModule, // imports firebase/firestore, only needed for database features |
| 124 | + AngularFireAuthModule, // imports firebase/auth, only needed for auth features, |
| 125 | + AngularFireStorageModule // imports firebase/storage only needed for storage features |
| 126 | + ], |
| 127 | + declarations: [ AppComponent ], |
| 128 | + bootstrap: [ AppComponent ] |
| 129 | +}) |
| 130 | +export class AppModule {} |
| 131 | +``` |
| 132 | + |
| 133 | +### 7. Inject `AngularFirestore` |
| 134 | + |
| 135 | +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): |
| 136 | + |
| 137 | +```ts |
| 138 | +import { Component } from '@angular/core'; |
| 139 | +import { AngularFirestore } from 'angularfire2/firestore'; |
| 140 | + |
| 141 | +@Component({ |
| 142 | + selector: 'app-root', |
| 143 | + templateUrl: 'app.component.html', |
| 144 | + styleUrls: ['app.component.css'] |
| 145 | +}) |
| 146 | +export class AppComponent { |
| 147 | + constructor(db: AngularFirestore) { |
| 148 | + |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### 8. Bind a Firestore collection to a list |
| 154 | + |
| 155 | +In `/src/app/app.component.ts`: |
| 156 | + |
| 157 | +```ts |
| 158 | +import { Component } from '@angular/core'; |
| 159 | +import { AngularFirestore } from 'angularfire2/firestore'; |
| 160 | +import { Observable } from 'rxjs'; |
| 161 | + |
| 162 | +@Component({ |
| 163 | + selector: 'app-root', |
| 164 | + templateUrl: 'app.component.html', |
| 165 | + styleUrls: ['app.component.css'] |
| 166 | +}) |
| 167 | +export class AppComponent { |
| 168 | + items: Observable<any[]>; |
| 169 | + constructor(db: AngularFirestore) { |
| 170 | + this.items = db.collection('items').valueChanges(); |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +Open `/src/app/app.component.html`: |
| 176 | + |
| 177 | +```html |
| 178 | +<ul> |
| 179 | + <li class="text" *ngFor="let item of items | async"> |
| 180 | + {{item.name}} |
| 181 | + </li> |
| 182 | +</ul> |
| 183 | +``` |
| 184 | + |
| 185 | +### 9. Run your app |
| 186 | + |
| 187 | +```bash |
| 188 | +ng serve |
| 189 | +``` |
| 190 | + |
| 191 | +Run the serve command and navigate to `localhost:4200` in your browser. |
| 192 | + |
| 193 | +And that's it! If it's totally *borked*, file an issue and let us know. |
| 194 | + |
| 195 | +### [Next Step: Documents in AngularFirestore](firestore/documents.md) |
0 commit comments