1. Introduction to Vue.js & Setup
Introduction to Vue.js
Welcome to the world of Vue.js 3. Vue is a progressive, approachable, and versatile JavaScript framework for building user interfaces. Unlike monolithic frameworks that force you into a specific architecture, Vue is designed to be incrementally adoptable. You can start by using it as a simple script tag to add interactivity to a legacy page, or you can go all-in with a full-stack Single-File Component (SFC) architecture.
At the core of Vue.js 3 is its reactivity system, which utilizes JavaScript Proxy objects. When you define data, Vue wraps it in a reactive proxy. This means that whenever your data changes, Vue automatically tracks those changes and updates the DOM (Document Object Model) efficiently. This is known as a virtual DOM system, which calculates the difference between your state and the current UI, patching only the necessary elements rather than re-rendering the entire page. This approach solves the performance bottlenecks associated with direct DOM manipulation, making it an essential tool for modern web development.
As you progress, you will learn to use ref and reactive, which are the primary primitives for managing component state. Understanding these is the difference between writing static HTML and dynamic, application-grade interfaces. We will focus on the SFC pattern, which packages HTML, CSS, and logic into a single .vue file, enforcing a clean separation of concerns within your components.
<!-- Simple Vue App Setup --> <div id="app">{{ message }}</div> <script type="module"> import { createApp, ref } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js' createApp({ setup() { const message = ref('Hello Vue 3!') return { message } } }).mount('#app') </script>
Vue vs Other Frameworks
When choosing a framework, developers often weigh the pros and cons of React, Angular, and Vue. Vue sits in a unique "sweet spot," offering a gentler learning curve than Angular and a more opinionated, out-of-the-box structure than React. While React relies heavily on JSX and manual optimization (like useMemo and useCallback), Vue handles these optimizations automatically through its reactivity compiler.
| Feature | Description |
|---|---|
| Reactivity | Vue uses mutable proxies; React uses immutable state and re-renders. |
| Syntax | Vue uses HTML-based templates; React prefers JSX/TSX. |
| Ecosystem | Vue provides official state management (Pinia) and routing (Vue Router). |
This comparison highlights why Vue is often the choice for teams that value developer experience (DX). By providing official libraries for the most common needs—routing and state management—Vue removes the "choice paralysis" often found in the React ecosystem. You are not just learning a library; you are learning an integrated, cohesive framework designed to work together seamlessly from day one.
<script setup> import { ref, computed } from 'vue'; const count = ref(0); // Vue automatically tracks 'count.value' dependency. // No manual dependency array needed! const doubleCount = computed(() => count.value * 2); </script>
Setting Up a Vue Project
To begin, we use create-vue, the official project scaffolding tool. This setup process ensures you have a modern build pipeline powered by Vite, which is an extremely fast build tool that uses native ES modules during development. Start by opening your terminal and running the following command:
npm create vue@latest
During the setup, you will be presented with a series of prompts. For this course, select the following:
- Project name: voidx-first-app
- TypeScript: No
- Vue Router: Yes
- Pinia: Yes
- ESLint: Yes
After the installation completes, you will have a directory structure like the one shown below:
voidx-first-app/ ├── node_modules/ ├── src/ │ ├── assets/ │ ├── components/ │ ├── router/ │ ├── stores/ │ ├── App.vue │ └── main.js ├── package.json └── vite.config.js
The package.json file contains your dependencies, while main.js serves as the entry point that mounts the Vue app instance to your index.html file. Vite manages the hot-module replacement, meaning that every time you save a file, your browser updates in milliseconds without losing application state.
Your First Vue Component
A Vue SFC (Single-File Component) follows the , , and structure. Let's create a counter component. Create a file named Counter.vue inside the src/components folder.
<script setup> import { ref } from 'vue'; const count = ref(0); const increment = () => count.value++; </script> <template> <div class="counter"> <h2>Count is: {{ count }}</h2> <button @click="increment">Add</button> </div> </template>
Line 2 imports ref from the Vue package. The ref function creates a reactive object that holds a value; whenever we change count.value, Vue detects the mutation and triggers a DOM update. The @click directive in the template is an event listener shorthand, mapping the button click directly to our increment function. Finally, the scoped attribute in the style tag ensures that these styles apply only to this specific component, preventing CSS leakage into other parts of your app.
Running a Vue App
To view your creation, you must start the development server. Navigate to your project folder in the terminal and execute the following command:
npm install npm run dev
Once you run npm run dev, Vite will provide a local URL, typically http://localhost:5173. Open this in your browser. You will see your application running instantly. The npm install command ensures that all dependencies listed in package.json are downloaded into node_modules. Without this step, your application lacks the necessary engine to execute the Vue runtime.
If you encounter issues, check the vite.config.js file or ensure your Node.js version is 18.0 or higher. Running your app in development mode allows you to use the Vue DevTools extension, an invaluable browser utility that lets you inspect your component hierarchy, track reactive state changes, and debug your Pinia stores in real-time. This feedback loop is essential for building robust, professional-grade software efficiently.
Deep Dive: Vue Instance & Reactivity Engine
To truly master Vue.js, you must understand the rules and edge cases of the createApp initialization and its underlying engine.
- createApp Parameters: It accepts a root component object. You can also pass a second argument for root props:
createApp(App, { prop1: 'value' }). - Mounting Edge Cases: When
app.mount('#app')is called, Vue replaces the innerHTML of the target container. If the container has existing DOM elements, they are completely overwritten unless you are using Vue for server-side rendering (SSR) hydration. - Proxy Limitations: Vue 3's reactivity uses ES6 Proxies. Unlike Vue 2, Proxies automatically track property additions and deletions. However, Proxies cannot intercept operations on primitive values (strings, numbers, booleans), which is precisely why Vue requires the
ref()wrapper to maintain a reference to the mutating value. - Browser Support: Because ES6 Proxies cannot be polyfilled, Vue 3 drops support for Internet Explorer 11 entirely.
// Edge case: Destructuring loses Proxy reactivity! const state = reactive({ count: 0 }) let { count } = state // count is now a disconnected primitive 0! count++ // Will NOT trigger UI updates.
Knowledge Check
Ready to test your understanding of 1. Introduction to Vue.js & Setup?