Mastering Vue 3 Project Structure: From Basics to Scalable Architecture
This guide walks you through organizing a Vue 3 application, starting with the default Vue CLI layout and progressively adding optimized folders such as assets, components, views, router, store, composables, services, and types, while offering practical tips for naming, lazy loading, and modularization to keep projects scalable and maintainable.
Imagine you are deep‑diving into a Vue project: it starts smooth, but as the app grows the folder tree becomes chaotic and finding files feels like searching for a needle in a haystack. An organized file structure is the backbone of scalable, maintainable, and efficient front‑end development, yet there is no one‑size‑fits‑all solution.
1. Start with the Vue CLI default structure
When you create a Vue project with Vue CLI, you receive a basic layout:
src/
├── assets/
├── components/
├── views/
├── router/
├── store/
├── App.vue
├── main.jsThis setup works well for small apps or prototypes, but as the application expands you will need to rethink several directories.
2. Core folders: explanation and optimization
assets/: "Correct handling of static files" – store images, fonts, and global styles here. Avoid mixing component‑specific resources; keep those inside the related component folder.
assets/
├── images/
├── fonts/
├── styles/Avoid placing component‑specific assets in this folder; keep them alongside the component. components/: "Modularization and organization" – for larger projects a flat structure becomes hard to manage. Group components logically:
components/
├── common/ # reusable components like buttons, modals
├── layout/ # header, footer, sidebar
├── dashboard/ # dashboard‑specific componentsPro tip: prefix shared components with Base (e.g., BaseButton.vue) to indicate they are reusable across the app. views/: "Top‑level pages" – these are route‑level pages that usually compose multiple components.
views/
├── Home.vue
├── About.vue
├── Dashboard.vuePro tip: use lazy loading for route‑based components to improve performance. router/: "Centralized navigation" – manages application routing. A typical index.js might look like:
import { createRouter, createWebHistory } from 'vue-router';
import Home from '@/views/Home.vue';
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/about', name: 'About', component: () => import('@/views/About.vue') },
];
export default createRouter({
history: createWebHistory(),
routes,
});Pro tip: for large apps, split routes into modules and import them into a central router file. store/: "State management with Pinia or Vuex" – if you use a store, keep global state here and break it into modules for readability.
store/
├── index.js # main store file
├── auth.js # authentication state
├── products.js# product‑related state3. Extensions: advanced folders to add
As the app grows, consider adding these directories for better organization: composables/: "Reuse logic with the Composition API" – extract reusable logic into custom hooks.
// composables/useFetch.js
import { ref } from 'vue';
export function useFetch(url) {
const data = ref(null);
const error = ref(null);
fetch(url)
.then(response => response.json())
.then(json => (data.value = json))
.catch(err => (error.value = err));
return { data, error };
} services/: "API calls and external logic" – centralize all HTTP requests and third‑party integrations.
// services/api.js
import axios from 'axios';
export const getProducts = () => axios.get('/api/products');
export const getUser = id => axios.get(`/api/users/${id}`); types/: "Type definitions (for TypeScript users)" – store interfaces and type declarations.
types/
├── User.ts
├── Product.ts4. File‑naming best practices
Use kebab‑case for file names: user-profile.vue Add a Base prefix for reusable components: BaseButton.vue Group related files together (e.g., component, style, test)
5. Real‑world example
A medium‑to‑large Vue 3 project might end up with the following structure:
src/
├── assets/
├── components/
│ ├── common/
│ ├── layout/
│ ├── dashboard/
├── composables/
├── router/
├── services/
├── store/
├── types/
├── views/
├── App.vue
├── main.jsConclusion
A thoughtfully designed file structure is more than aesthetic; it is essential for building an elegant, growing project that makes life easier for you and your team. With this guide, you can create a Vue 3 architecture that is both scalable and maintainable.
What will your next project look like? Ready to turn Vue chaos into order? Let’s start building!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
