Introduction to Vue.js

Introduction to Vue.js

This is for a complete beginner. A Vue.js guide for dummies!

Table of Contents

  1. What is Vue?
  2. Why should you learn Vue.js?
  3. Getting Started - Installation Methods
  4. First Vue App - Build Hello World!

What is Vue.js?

"Vue (pronounced /vjuː/, like view) is a progressive framework for building user interfaces"

This is the introduction from the official Vue.js documentation. The Vue.js framework is robust and versatile. It could be integrated with an existing project and is also capable of handling complex single-page applications.

This is an open-source JavaScript framework created by Evan You. It was initially released in February 2014. This framework was inspired by the Model-View-ViewModel (MVVM) which separates user interface development from the development of back-end logic.

It is highly recommended to have a basic understanding of HTML, CSS and JavaScript before diving into Vue.js or any other frameworks.

FreeCodeCamp's JavaScript course helped me brush up on Javascript fundamentals when I was starting out with Vue.

Alternatively, you could bookmark this MDN page for quick refresher/reference.

Why should you learn Vue.js?

  • The learning curve for Vue is very easy.
  • Vue.js documentation is one the best! It's easy to understand for any skill level.
  • Virtual DOM manipulations can update specific parts without the whole page refreshing.
  • Vue.js is one of the most loved frameworks by developers.

Getting started

CDN

We could initiate by placing a script tag in an HTML file to load the latest version of Vue from the Content Delivery Network (CND)

Suitable for prototyping or learning purpose

<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>


<!-- production version, optimized for size and speed -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>

NPM

This method is recommended for developing large scale applications.

# latest stable
$ npm install vue

CLI

For a quick scaffolding Vue-CLI can get a projected start within minutes. This method is not recommended for beginners without prior experience with Node.js.

"Scaffolding is a meta-programming method of building database-backed software applications. It is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used."

First Vue App

For the purpose of learning, let us follow the CDN method. We'll will need a standard .html file and a .js file.

Let's name them index.html and main.js respectively.

A javascript file is where we'll create a new application of Vue. Then, mount this application on the .html file.

A new Vue instance is created by declaring the new Vue({ ... }) constructor.

Initiating an instance of Vue

Here is the code for our main.js file:

new Vue({
   el: '#app',
   data: {
    message: 'Hello World!',
    },
});

vuejs diagram 01 (1).png

Let's look into the elements and understand each line:

  1. A new Vue instance is created with the constructor. This brings the Vue app to life.
  2. el: '#app' is the element option, which is reflected in the HTML file with the same name as an id. This is will be the mounting point of this Vue application.
  3. Vue instance accepts an object with multiple options. Which can contain details of the instance such as el,data,methods`, etc. These options are used to create desired behaviour of the Vue component.
  4. data: {message: 'Hello World!'} is the data option with one String variable with a value. This property with key-value pair is used to reference in the .html body to dynamically display the current value.

In other words, this is how we are printing 'Hello World!' on the HTML page using Vue.

What makes this special?

Suppose this value was updated based on user interaction, the changes would reflect on the web page without having to update the page.

More on this Vue's power to make changes on DOM later in this series! Stay tuned.

Mounting on HTML

Now we have our Vue app ready for mounting.

<HTML>
 <body>
  <div id="app">
    <h2>{{ message }}</h2>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script src="./main.js"></script>
 </body>
</html>

vuejs diagram 02 (1).png

Here is the anatomy of the .html file to initiate the Vue app:

  1. The element #app is declared as an id where we want the Vue app to be mounted.
  2. The Mustache syntax {{ ... }} is used to display the value of the property from the data option. This binds the data values and prints as the text content of the element.
  3. CDN is one method of installing Vue.js, here we are using the development version to install Vue.js with the script tag.
  4. main.js is imported with the script tag to bring in the Vue app functionality here.

Example

In this example shared on Vue.js docs, the Vue app is initiated in the .html file itself without a separate JavaScript file.


In the coming days, we'll dive deeper and explore the magic of Vue!

Resources

Did you find this article valuable?

Support Raif's Tech Blog by becoming a sponsor. Any amount is appreciated!