Getting Started with Vue 2

Learn about Vue 2.0, the MVVM pattern, and common installation methods to get started with Vue development.

🎯 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.

An everyday analogy for MVVM:Imagine you are writing a letter (the View) that contains the information you want to communicate (the Model). A secretary (the ViewModel) turns your ideas into a neatly written letter. When you change the content, the secretary immediately updates the text; when a reader provides feedback, the secretary passes it back to you. This illustrates the core idea of MVVM.

🚀 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>
The CDN approach is useful for quick experiments and learning, but maintenance and performance optimization become less convenient as a project grows.

⚙️ 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.

  1. Install Vue CLI globally using npm or yarn.
yarn global add @vue/cli
  1. Create a new Vue 2 project.
vue create my-vue-app
  1. 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
  1. Change to the project directory and run the following command to start the project.
npm run serve
  1. Open http://localhost:8080 in your browser to see the Vue 2 application running.
Vue CLI is suitable for application development. It supports components and modules, making ongoing project maintenance easier.

❓ 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

  1. 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>
  1. After creating a project with Vue CLI, edit the template in App.vue to 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!