Getting Started with Vue 2
🎯 Introduction
This article introduces the core ideas and design patterns behind Vue 2.0, shows you several ways to install it, and helps you get started with frontend development. Vue 2.0 is a widely used progressive JavaScript framework for building interactive web applications. Understanding its fundamentals and installation options gives you a foundation for building reactive frontend projects.
🔍 Introducing Vue 2.0
Vue 2.0 is a progressive JavaScript framework focused on building user interfaces. Released by Evan You in 2016, it quickly gained widespread attention and adoption thanks to its flexibility, efficiency, and approachable learning curve.
Vue lets developers build pages declaratively. Its concise template syntax and powerful reactivity system simplify frontend development. Compared with other large frameworks, Vue 2.0 is lightweight and flexible: you can use it to build single-page applications or adopt it incrementally in an existing project.
The main strengths of Vue 2.0 include:
- Reactive data binding: data changes automatically update the view without manual DOM manipulation.
- Component-based development: reusable components improve code organization and maintainability.
- Ease of learning: HTML templates and JavaScript logic make it accessible to beginners.
- A rich ecosystem: official companion libraries such as Vue Router and Vuex support a wide range of needs.
🧩 The MVVM Pattern
Vue 2.0 follows the MVVM (Model-View-ViewModel) design pattern. The name may sound complicated, but the idea is straightforward: manage data and the interface separately to keep code clearer and easier to maintain.
What Do the Three Parts of MVVM Represent?
- Model: your data, such as user information or a list of products.
- View: the interface users see, built with HTML and CSS.
- ViewModel: the intermediary between the Model and the View. It observes data changes and automatically updates the view, while also reflecting user interactions back into the data.
This design offers two advantages:
- You can focus on data changes while the view updates automatically, without writing complex DOM manipulation code.
- Decoupling data from the view improves code reuse and testability.
🚀 Installing Vue 2
There are several ways to install and use Vue 2. The two main approaches are loading it from a CDN and using the Vue CLI scaffolding tool.
🌐 Loading Vue from a CDN
This is the simplest approach. No build tools are required: add a <script> tag that loads the Vue library in an HTML file, and you can start using it immediately.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vue 2 CDN Example</title>
</head>
<body>
<div id="app">{{ message }}</div>
<!-- Load Vue 2.x from a CDN -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello Vue 2.0!',
},
});
</script>
</body>
</html>
⚙️ Installing with Vue CLI
Vue CLI is the official scaffolding tool for generating a Vue project. It supports modern development features such as modular development and hot reloading. It requires Node.js; Vue CLI 4.x requires Node.js v8.9 or later.
- Install Vue CLI globally using npm or yarn.
yarn global add @vue/cli
npm install -g @vue/cli
- Create a new Vue 2 project.
vue create my-vue-app
- When prompted, select Vue 2 and choose npm or yarn to install dependencies. The CLI generates a complete project with webpack, Babel, ESLint, and other configuration.
Vue CLI v5.0.8
? Please pick a preset: (Use arrow keys)
Default ([Vue 3] babel, eslint)
❯ Default ([Vue 2] babel, eslint)
Manually select features
- Change to the project directory and run the following command to start the project.
npm run serve
- Open
http://localhost:8080in your browser to see the Vue 2 application running.
❓ Questions and Answers
Q1: Why does Vue 2.0 use the MVVM pattern?
MVVM separates data from the view and automatically updates the view when data changes, simplifying frontend development.
Q2: When is the CDN approach useful?
It is useful for simple demos and quick tests, but is less suitable for developing large projects.
Q3: How do you create a Vue 2 project with Vue CLI?
After installing Vue CLI, choose manual configuration when creating a project and select Vue 2.
🧪 Exercises
- Create a simple Vue application using a CDN that dynamically displays the current time.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vue Time Display</title>
</head>
<body>
<div id="app">Current time: {{ currentTime }}</div>
<!-- Load Vue 2 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
currentTime: '',
},
mounted() {
// Implement live time updates here
},
});
</script>
</body>
</html>
- After creating a project with Vue CLI, edit the template in
App.vueto display “Welcome to Vue 2.0”.
🎉 You now understand the basics of Vue 2.0, the MVVM pattern, and installation methods. Start writing code and explore what Vue can do!
Third-Party Library Types and Declarations
Learn to use community type packages, understand .d.ts files, write declarations, and work with libraries that do not provide types.
Template Syntax and Directives
Learn template syntax, common and custom directives, filters, and dynamic class and style bindings through beginner-friendly examples.
