You must also install Vue.js. Check out the instructions page from the official Vue documentation at https://vuejs.org/v2/guide/installation.html. Alternatively, simply run the npm install
command:
Now, everything is ready to start scaffolding our application. Go to the folder where you want your application to reside and type the following line of code:
It will ask you several questions. Just choose the default answer and hit Enter for each of them. After the initialization, you are ready to install and run your application:
If everything is fine, the following page will automatically open in your default browser:
If not, check the Vue.js official installation page again.
Connecting the Vue.js application to the Firebase project
To be able to connect your application to the Firebase project, you must install Firebase and VueFire. Run the npm install
command while being in the root directory of your new application:
Now, you can use Firebase's powerful features inside your application. Let's check if it worked! We just have to do the following:
- Import Firebase
- Create a
config
object containing the Firebase app ID, project domain, database domain, and some other stuff needed to connect it to our project - Write the code that will use the Firebase API and the created
config
file to connect to the Firebase project. - Use it
Where do we get the necessary information for the configuration of our Firebase instance? Go to the Firebase console, click on the cog to the right of the Overview tab, and select Project Settings. Now, click on the Add Firebase to your web app button:
A popup with all the information we need will open:
OK, now, just leave this popup open, go to your Vue application, and open the main.js
file that resides in the src
directory of your application. Here, we need to tell our Vue application that it will use VueFire. In this way, we will be able to use all the features provided by Firebase inside our application. Add the following lines to the import section of the main.js
file:
Great! Now, open the App.vue
file. Here, we will import Firebase and initialize our Firebase application inside the Vue application. Add the following lines of code inside the <script>
tags:
Copy what's needed for the config
object information from the popup that we opened in the previous step.
Now, we will obtain the reference to our messages database object. It is pretty simple using the Firebase API:
We're almost done. Now, we just have to export the messages
object in the Vue data object so that we are able to use it inside the template section. So, inside the export
section, add an entry with the firebase
key and point messages
to messagesRef
:
Now, inside the <template>
tag, we will use a v-for
directive to iterate through the messages
array and print all the information about each message. Remember that each message is composed of title
, text
, and timestamp
. So, add the following <div>
to the template:
In the end, your App.vue
component will look like this:
If you had chosen the default linter settings on the app initialization, the code that you will copy from Firebase and paste into your application will not pass linter. That's because the default linter settings of Vue-cli initialization would require the use of single quotes and no use of semicolon at the end of the line. By the way, Evan You is particularly proud of this no semicolon rule. So, bring him this pleasure; remove all the semicolons from the copied code and replace the double quotes with single quotes.
Aren't you curious to check out the page? If you are not running your application already, switch inside the application folder and run it:
I am pretty sure that you are seeing the following screenshot:
Congratulations! You have successfully completed the first part of our tutorial, connecting the Vue.js application to the Firebase real-time database.