Getting Started with Vue.js

Vue.js is a progressive JavaScript framework used for building user interfaces. Unlike monolithic frameworks, Vue is designed to be incrementally adaptable, meaning you can integrate it into projects a little at a time, or build fully-fledged web applications with it.
What is Vue.js?
Vue.js was created by Evan You in 2014 and has since become one of the most popular front-end frameworks due to its simplicity and flexibility. It focuses on the view layer, making it easy to integrate with other libraries or existing projects. You can also use it as a complete solution for building modern single-page applications (SPAs) when combined with tools like Vue Router and Vuex.
Why Use Vue.js?
Vue.js offers the best of both worlds – it combines the declarative nature of React with the more template-driven approach of Angular. Here are some reasons why Vue.js has become so popular:
- Simplicity: Vue.js is designed to be easy to pick up. You can integrate it into an existing project without much hassle.
- Reactivity: Vue has a reactive data-binding system that updates the DOM when your app’s state changes.
- Component-based Architecture: Similar to React, Vue encourages you to break down your UI into smaller, reusable components.
- Performance: Vue is lightweight and efficient, making it a great choice for building high-performance web apps.
In this article, we’ll explore the following concepts:
- Setting Up Vue.js
- Vue Instance and Data Binding
- Directives and Conditionals
- Components and Props
- Vue Router for Single-Page Applications
- State Management with Vuex
- Vue CLI for Scaffolding Projects
Setting Up Vue.js
There are multiple ways to start working with Vue.js:
- Directly in the browser: You can include Vue via a CDN link in your HTML file and start using it with just a
<script>tag. - Vue CLI: For larger projects, Vue CLI provides a fully-featured setup with build tools like Webpack, hot-reload, and linting.
- Single File Components (SFC): With Vue’s single-file components, you can encapsulate HTML, CSS, and JavaScript logic in one
.vuefile.
Here’s how to include Vue in your HTML file using a CDN:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vue.js Example</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">{{ message }}</div>
<script>
new Vue({
el: "#app",
data: {
message: "Hello Vue.js!",
},
});
</script>
</body>
</html>
This will render the message “Hello Vue.js!” in the div with the id of app.
Vue Instance and Data Binding
The Vue instance is the heart of every Vue application. It connects your data to the DOM, and whenever the data changes, the DOM updates automatically. Vue's data binding is two-way, meaning any changes made in the input fields or elsewhere in the DOM are reflected in the app’s data.
Here's an example of basic data binding:
<div id="app">
<input v-model="message" placeholder="Type something" />
<p>The message is: {{ message }}</p>
</div>
<script>
new Vue({
el: "#app",
data: {
message: "",
},
});
</script>
As you type in the input field, the paragraph content will automatically update to show the message.
Directives and Conditionals
Vue.js comes with built-in directives that add functionality to your HTML elements. For instance, v-bind is used for binding attributes, and v-if is used for conditional rendering.
Here's an example of conditional rendering using the v-if directive:
<div id="app">
<p v-if="isVisible">This message is visible</p>
<button @click="toggleVisibility">Toggle Message</button>
</div>
<script>
new Vue({
el: "#app",
data: {
isVisible: true,
},
methods: {
toggleVisibility() {
this.isVisible = !this.isVisible;
},
},
});
</script>
In this example, the paragraph will only be visible if isVisible is true, and the button will toggle its visibility.
Components and Props
Components are reusable Vue instances with their own isolated scope. They allow you to break your UI into smaller, manageable pieces. Each component can have its own data and methods, and can communicate with other components using props and events.
Here’s an example of a simple Vue component:
<div id="app">
<greeting message="Hello from Vue component!"></greeting>
</div>
<script>
Vue.component("greeting", {
props: ["message"],
template: "<h2>{{ message }}</h2>",
});
new Vue({
el: "#app",
});
</script>
In this case, the greeting component takes a message prop and displays it inside an h2 tag.
Vue Router for Single-Page Applications
Vue Router is the official router for Vue.js. It enables you to map your components to different routes in your application, making it ideal for building single-page applications (SPAs).
Here’s an example of setting up Vue Router:
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "./components/Home.vue";
import About from "./components/About.vue";
Vue.use(VueRouter);
const routes = [
{ path: "/", component: Home },
{ path: "/about", component: About },
];
const router = new VueRouter({
routes,
});
new Vue({
el: "#app",
router,
});
This sets up a simple router with two routes: one for the home page and another for the about page.
State Management with Vuex
For managing the state of larger Vue applications, Vue provides Vuex, a state management library. Vuex allows you to centralize your application's state and make it accessible to all components.
Here’s a simple Vuex store setup:
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: 0,
},
mutations: {
increment(state) {
state.count++;
},
},
});
new Vue({
el: "#app",
store,
computed: {
count() {
return this.$store.state.count;
},
},
methods: {
increment() {
this.$store.commit("increment");
},
},
});
This setup allows components to read from the Vuex store and update it using mutations.
Vue CLI for Scaffolding Projects
For larger projects, you can use the Vue CLI to scaffold a project with a pre-configured build system, hot-reload, linting, and other useful tools.
To install Vue CLI and create a project:
npm install -g @vue/cli
vue create my-project
Vue CLI allows you to quickly generate a new Vue project with a full development environment, making it easy to start building large-scale Vue applications.
Conclusion
Vue.js is a powerful and flexible framework that simplifies the process of building interactive user interfaces. Whether you are creating simple websites or complex single-page applications, Vue.js provides the tools and flexibility to scale your project efficiently.

With its component-based architecture, reactivity, and powerful tools like Vue Router and Vuex, Vue.js is a fantastic option for front-end development. Whether you’re a beginner or an experienced developer, you’ll find Vue.js to be a valuable addition to your toolkit.
This extended version covers the basics of Vue.js and introduces key concepts like components, directives, Vue Router, and Vuex for managing state in larger applications.