Vue.js is a progressive JavaScript framework designed for building user interfaces. Unlike other comprehensive frameworks, Vue is constructed to be incrementally adoptable. Its core library concentrates exclusive on the view layer, which makes it both simple to learn and straightforward to integrate with other libraries or existing projects. Furthermore, when paired with modern development toolchains and supporting libraries, Vue is fully capable of driving sophisticated single-page applications.
Before diving into Vue, it's essential to have Node.js and its package manager, NPM, installed. Node.js provides a JavaScript runtime environment, while NPM is the tool for managing project dependencies. You can download the latest Long-Term Support (LTS) version from the official Node.js website. After installation, confirm the setup by running node -v and npm -v in your terminal.
There are two main ways to incorporate Vue into a project. For rapid prototyping, you can include it directly via a Content Delivery Network (CDN) link in your HTML file. For more structured development, it's recommended to use NPM to manage dependencies.
Here is the CDN link for the development version, which includes helpful command-line warnings:
<!-- Development version with helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
And for production, use the optimized version:
<!-- Production version, optimized for size and speed -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
Getting Started with a Basic Example
Let's create a simple HTML file to demonstrate Vue's core functionality. We'll render a message from our data model into the DOM.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.js Basics</title>
</head>
<body>
<div id="app">
<p>The message is: {{ greeting }}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
// Create a new Vue instance
const myApp = new Vue({
// The 'el' property specifies the DOM element to mount the instance
el: '#app',
// The 'data' object holds the reactive data for the application
data: {
greeting: 'Welcome to Vue!'
}
});
</script>
</body>
</html>
In this example, we create a Vue instance named myApp. The el property targets the <div> with the ID app. The data object contains a property called greeting, which is then rendered into the paragraph tag using the double curly brace syntax {{ greeting }}.
Demonstrating Two-Way Data Binding
Vue's powerful feature of two-way data binding allows you to synchronize data between the model and the view seamlessly. Let's see how an input field can update a data property in real-time.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Two-Way Binding</title>
</head>
<body>
<div id="app">
<input type="text" v-model="userName">
<p>Hello, {{ userName }}!</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
const myApp = new Vue({
el: '#app',
data: {
userName: 'Guest'
}
});
</script>
</body>
</html>
Here, the v-model directive on the input field creates a two-way binding with the userName data property. As you type in the input, the value of userName is updated, and the paragraph tag automatically reflects this change.
Handling User Events
You can easily handle user interactions like clicks using Vue's event handling directives. Let's create a button that increments a counter.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Handling</title>
</head>
<body>
<div id="app">
<button @click="increment">Click Me</button>
<p>You've clicked the button {{ clickCount }} times.</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
const myApp = new Vue({
el: '#app',
data: {
clickCount: 0
},
// The 'methods' object contains functions to be called on events
methods: {
increment() {
this.clickCount++;
}
}
});
</script>
</body>
</html>
In this final example, we use the @click (a shorthand for v-on:click) directive to listen for a click event on the button. When clicked, it calls the increment method defined in the methods object, which in turn updates the clickCount data property. The change is immediately reflected in the paragraph tag.