Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • like 13 26

    Building With Vue.js 2 and Firebase

    Introduction

    Firebase is Google's mobile platform that helps you develop high-quality apps and grow your business. In this tutorial, you will make good use of one of Firebase's awesome features: the Realtime Database. 

    You will build a single page application to create books. This book will be saved to your Firebase database, and you will be able to retrieve and delete books you have created.

    Let's get started.

    Set Up Firebase

    Go to Google's Firebase page to create a new account. When done with that, log in to your console. Click on the option to add a project. Enter your project details and click on the button CREATE PROJECT.

    This will lead you to your console. The Firebase console helps you manage your Firebase configuration settings.

    For this tutorial, you'll need to make access to your database public. From the panel on the left, select Database. Select Realtime Database from the options that show next by clicking GET STARTED. Making your database public involves editing the rules. So click RULES on the page that loads next.

    Make your rules look like this.

    Click the option to PUBLISH when done.

    With this rule, authentication is not required to perform read and write actions on your database. This is needful for the application you will be building in this tutorial.

    Set Up a Project Using Vue CLI

    Vue CLI allows you to scaffold Vue.js projects. If you do not have it on your machine, you can get it by running:

    This will install it globally on your machine. Here is how Vue-CLI is used.

    To learn more about Vue-CLI, check the GitHub page.

    For this project you will use webpack templates, so run the command below from your terminal.

    These are the installation options I used.

    Navigate to your project folder. The files and folders generated by Vue-CLI have a tree like this.

    Now run the command to install your dependencies.

    When done, you can start your dev server by running:

    Add Firebase to the Project

    To bind Firebase data to Vue.js data properties, we will make use of the VueFire library. You can check more about it on GitHub.

    Run the command below:

    Open up main.js to add VueFire. Make your main.js file look like what I have below.

    Set Up the Firebase Connection

    Go to your Firebase console, and click on the Overview link on the left panel. Select the option to add Firebase to your web app. Copy the snippet that pops up in the window to a text file. The snippet contains your apiKey, authDomain, databaseURL, projectId, storageBucket, and messagingSenderId. You need these details to be able to access your Firebase database.

    You start by importing Firebase from the core Firebase library. A Firebase instance is created using the initializeApp method. The snippet you copied has to be passed to this method as an object. This has to be done in the script section of your App.vue, like this.

    After creating the Firebase instance, the database reference is obtained by using app.database().

    Book Listing

    Since VueFire makes it easy to bind Vue.js data properties to Firebase, implementing the books listing feature requires you to add this.

    You add that below:

    Now you have access to the book items from your database. The template will look like this.

    The v-for directive is used to iterate through the available books. Each book will be outputted in a new table row.

    Adding a New Book

    To put in place the addition of new books, you need to first define the data model that will be used.

    Next, set up the template to look like this.

    The v-model directive is used to bind the newBook properties to the corresponding input.

    The v-on directive will lead us to create an event handler method that gets called whenever a new book is to be created. Here is what the event handler should look like.

    The addBook method helps insert new book objects into the Firebase database. The data is also synced across all clients. 

    Deleting Books

    Let's add the ability to delete books. Add another column to the book listing.

    Let's put in place a method that gets called each time the button is clicked. The method is passed the book you intend to delete, which is actually the key to the book, as you will see soon. The remove() is called on the returned book to delete it from the database.

    Here is what the method looks like.

    With that, you are done with App.vue. Putting everything together, here is how your App.vue file should look.

    In the template, I added some Bootstrap classes. For these to work, open your index.html file and make it look like this.

    Conclusion

    JavaScript has become extremely popular and is now capable of building mature applications (as we've seen above). If you’re looking for additional resources to study or to use in your work, check out what we have available in the Envato Market.

    In this tutorial, you learned about Firebase. You were able to connect Vue.js and Firebase using VueFire. Your application can make read and write requests to your Firebase database.

    You can go further by adding more features like categories and description.

    martijn broeders

    founder/ strategic creative at shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 434 210 0245
  • 1
    silhouette
    Peden
    September 04, 2018 at

    Hello! I'm at work browsing your blog from my new apple iphone!Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the great work!

  • 2
    silhouette
    cameron
    September 04, 2018 at

    Very great post. I just stumbled upon your weblog and wanted to mention that I have truly enjoyed browsing your blog posts. After all I'll be subscribing on your feed and I hope you write again soon!

  • 3
    silhouette
    juliane mckibben
    November 20, 2017 at

    WOW just what I was looking for. Came here by searching for shineyrock

  • 4
    silhouette
    carissa
    November 19, 2017 at

    That is a good tip especially to those fresh to the blogosphere. Simple but very accurate info… Appreciate your sharing this one.A must read post!

  • 5
    silhouette
    Audry
    November 17, 2017 at

    You should take part in a contest for one of the finest sites on the internet.I most certainly will highly recommend this website!

  • 6
    silhouette
    marilyn
    November 15, 2017 at

    These are really enormous ideaas in concerning blogging. You have touched some nice things here. Any way keep up wrinting.

By - category

    By - date