What's up guys, Eze is here. Those who follow me on Twitter (if you are not a follower you better do it now @skielo) already know I've been working on an app to show you how to use Firebase from an Angular 2 app. I have several posts talking about Firebase and how to use almost all its functionalities from an AngularJS app, in case you missed it you can check it here. Today, I want to show you how easy is to get an Angular 2 app up and running and connected to Firebase. This will be the first out of a series of posts where we are going to find out more about those tools.

The Skeleton of Our App

I want to show you first how to create a new Angular 2 app from the scratch. To do that we will need to install the angular cli tool onto our machines. The installation of this tool is really simple - we just need run the following command npm install -g @angular/cli. Please be sure you have a node version higher than 4 because it's a requirement for this tool to run.

This tool will provide us lots of functionalities that we are going to use during the development of our app but first, we need to create the scaffolding of our app, by executing the following command:ng new app_name. The Angular cli will now create a new Angular 2 app. We can see our app running by executing the ng serve command.

Adding Angular Fire 2 and Firebase to Our Project

We already have the basics of our project, so it's time to add the reference to Angular Fire 2 and Firebase. We will do it by running the following command inside our project folder:npm install angularfire2 firebase --save.This command is going to install all the necessary files in our project. So now it's time to dive into our app.module.ts in order to configure our Firebase project. First, we need import AngularFireModule and then export the configuration we get from Firebase.

Creating a Database Layer

Now that we have configured Firebase to our project, it's time to create our database layer in order to have access to our database. Again we are going to use Angular cli by typing ng generate service databaseservice. This time the command is going to generate a new service but you need to take care of something else, which is the warning we got from the tool: "WARNING Service is generated but not provided, it must be provided to be used." In order to provide this new service, we need to include it in the AppComponent as I did below.

As you can see in the code above, we use a great feature of TypeScript that allows us to define a property in the constructor which means that Angular will automatically inject the right object for us.

And that's it, after doing that we are ready to go. This AngularFire object will give us access to all Firebase functionality. In this example, I'm using the database object, in particular, the list method. As you can see, we need to pass the path reference to this method and the return will be a FirebaseListObservable object that will let us work with our database.

If you want to see the full code working together I have a working example here. This example is a small version of the game battleship.

For a complete reference of Angular Fire 2 use this link. For a complete reference of Firebase use this link.

If you found this post useful please don't forget to press the like button and share it. If you are in doubt don't hesitate to ask a question and, as always, thank you for reading!