Vue 3 Overview and Ecosystem Updates
🎯 Introduction
By the end of this article, you will understand the key differences between Vue 3 and Vue 2, use the updated global APIs, create a Vue 3 project with Vite, and understand its directory structure and hot module replacement. These foundations will help you get started with modern frontend development.
✨ Why Learn Vue 3?
Vue 3 is a major release of Vue.js that introduces performance improvements and new APIs, making it a popular choice for modern frontend development. Learning Vue 3 helps you write efficient, maintainable code and use tools such as the Composition API to organize component logic and improve reuse.
Vue 3 also offers better TypeScript support and works with newer build tools such as Vite for a smoother development experience. As new projects and major open-source libraries adopt the Vue 3 ecosystem, familiarity with it is an essential frontend skill.
🔄 Key Differences from Vue 2
The main changes from Vue 2 to Vue 3 fall into the following areas:
- Better performance
Vue 3 rewrites the reactivity system using Proxy instead of Object.defineProperty, improving performance and the range of changes it can detect. Startup and updates are faster, with lower memory usage. - Composition API
One of Vue 3's main features, the Composition API offers more flexible logic reuse than Vue 2's Options API. Like React Hooks, it helps organize complex component logic. - Better TypeScript support
Vue 3 is designed with TypeScript in mind, providing more accurate type inference and a smoother experience when writing Vue with TypeScript. - Changes to global APIs
Global registration APIs such asVue.componentandVue.usemove to application instances, aligning with modern modular development. - Updates to built-in components and directives
New capabilities include multiplev-modelbindings andTeleport, which renders component content at a specified DOM location. - Tree shaking
Vue 3 supports importing only what you need. Unused code can be excluded from the final bundle, reducing its size.
🌐 Global API Changes
Vue 3 changes how global APIs are organized. Instead of attaching them directly to the Vue constructor as in Vue 2, it exposes them on an application instance returned by the createApp factory function.
For example, registering a global component in Vue 2 looks like this:
Vue.component('MyComp', {
template: '<div>I am a global component</div>',
});
In Vue 3, it becomes:
import { createApp } from 'vue';
import App from './App.vue';
import MyComp from './MyComp.vue';
const app = createApp(App);
app.component('MyComp', MyComp);
app.mount('#app');
Here, createApp creates an application instance, and application-wide APIs operate on that instance.
Vue 3 also replaces Vue.use with app.use() for registering plugins; a plugin's install method can also be invoked manually.
Global configuration is another important change. It is now accessed through app.config, for example:
app.config.globalProperties.$http = axios;
This makes $http available to all components.
🚀 Creating a Vue 3 Project (Vite, with a Brief Look at Vue CLI)
Vite is the recommended build tool here for creating Vue 3 projects. Its fast startup, responsive hot updates, and use of native ES modules improve development efficiency.
Start a Vue 3 + Vite project with the following commands:
npm create vite@latest my-vue3-app
cd my-vue3-app
npm install
npm run dev
What the commands do:
npm create vite@latest: scaffold a project using the latest version of Vite.npm run dev: start the development server with hot module replacement.
Creating a Vue 3 Project with Vue CLI (Briefly)
Vue CLI also supports Vue 3, though startup and builds are slower than with Vite. Create a project with:
vue create my-vue3-app
Select Vue 3 when prompted. For new projects, Vite is the preferred choice.
📁 Directory Structure & Hot Module Replacement
A Vue 3 project created with Vite typically has this structure:
my-vue3-app/
├─ public/ // Static assets copied directly to the build directory
├─ src/
│ ├─ assets/ // Images, fonts, and other assets
│ ├─ components/ // Component directory
│ ├─ App.vue // Root component
│ ├─ main.js // Entry point that initializes the Vue application
├─ index.html // HTML entry point
├─ package.json // Project metadata and dependencies
├─ vite.config.js // Vite configuration
Hot Module Replacement (HMR)
Vue and Vite use hot module replacement (HMR) with ES modules in the browser. When you edit a component file, Vite sends the updated module to the browser, which replaces it without reloading the entire page.
This preserves application state and makes development smoother.
For example, editing the template in src/components/HelloWorld.vue immediately updates the browser without a page reload.
❓ Questions and Answers
Q1: Why does Vue 3 replace Object.defineProperty with Proxy?
Proxy can detect a wider range of object changes, offers better performance, handles nested objects more naturally, and allows simpler code.
Q2: How do Vite and Vue CLI differ?
Vite starts faster, provides efficient hot updates, and uses native ES modules for a more modern development experience. Vue CLI relies on Webpack, with more complex configuration and slower startup.
Q3: What advantages does the Composition API have over the Options API?
It groups logic by feature, makes code easier to reuse, and separates component concerns more clearly, which is useful in large, complex projects.
🧪 Exercises
Exercise 1
Create a Vue 3 project with Vite yourself. You can start with JavaScript while getting familiar with the workflow.
🎉 You now understand Vue 3's key changes, global APIs, project setup with Vite, and hot module replacement! Put these skills to use in your next modern frontend application.
