Vue components are the building blocks of any Vue application, but without a clear structure, managing them can become challenging as your app grows. In this article, we’ll explore best practices for structuring Vue components and organizing your folder structure for scalability and maintainability.
As soon as you start learning Vue, you discover that Vue components are the atomic units of its architecture. This isn't unique to Vue—other frameworks like React and Angular operate similarly.
Initially, you focus on building and combining components, applying Vue components communication patterns, and keeping them small for reusability. However, as your application grows, the increasing number of components can make it harder to maintain your project. Finding the right component or understanding its purpose becomes challenging, leading to a messy and less maintainable structure.
No worries—this article will guide you through practical strategies and tips for structuring Vue components effectively to keep your project organized and scalable.
When starting a Vue application, you'll typically begin with a components
folder alongside a main.js
entry file and an App.vue
component:
src/
main.js
App.vue
components/
Over time, your application will grow, and you'll create several components. A simple folder structure might look like this:
components/
ArticlePage.vue
ArticleTitle.vue
ArticleList.vue
AppButton.vue
AppFooter.vue
AppHeader.vue
LastArticlesSection.vue
AppList.vue
UserPage.vue
Note: Following Vue’s style guide for multi-word component names is recommended to avoid conflicts with native HTML tags. Prefix generic components with "App" or a similar convention.
To enhance organization, one of the first steps is to categorize components by their roles. For example, components like ArticlePage and UserPage can be classified as page components, so creating a pages
folder makes sense:
components/
ArticleTitle.vue
ArticleList.vue
AppButton.vue
AppFooter.vue
AppHeader.vue
LastArticlesSection.vue
AppList.vue
pages/
ArticlePage.vue
UserPage.vue
This approach simplifies navigation, making it easier to locate page-related components. For instance, if ArticleTitle and LastArticlesSection are unique to ArticlePage, grouping them under the same folder creates logical associations:
components/
ArticleList.vue
AppButton.vue
AppFooter.vue
AppHeader.vue
AppList.vue
pages/
ArticlePage/
index.vue
ArticleTitle.vue
LastArticlesSection.vue
UserPage.vue
Renaming ArticlePage.vue to index.vue simplifies imports, as Vue's module resolution automatically resolves to index.vue
within a folder:
import ArticlePage from "@/pages/ArticlePage"
Components with mixed responsibilities, such as AppButton or AppList, can be categorized as UI components. These reusable, logic free components rely on props and events for communication. Placing them in a ui
folder improves clarity:
components/
ui/
AppButton.vue
AppList.vue
ArticleList.vue
AppFooter.vue
AppHeader.vue
pages/
ArticlePage/
index.vue
ArticleTitle.vue
LastArticlesSection.vue
UserPage.vue
Components like AppFooter and AppHeader, used in layouts, can be moved to a layout
folder:
components/
layout/
AppFooter.vue
AppHeader.vue
ui/
AppButton.vue
AppList.vue
ArticleList.vue
pages/
ArticlePage/
index.vue
ArticleTitle.vue
LastArticlesSection.vue
UserPage.vue
Domain components, such as ArticleList, are reusable within a specific domain but not across the entire app. Creating domain-specific folders helps maintain order:
components/
article/
List.vue
layout/
AppFooter.vue
AppHeader.vue
ui/
AppButton.vue
AppList.vue
pages/
ArticlePage/
index.vue
ArticleTitle.vue
LastArticlesSection.vue
UserPage.vue
Simplify imports by removing redundant prefixes, as the folder path already clarifies the context:
import ArticleList from "@/components/article/List"
import AppList from "@/components/ui/AppList"
Some components, such as Observer or NoSSR, don’t fit into ui
, layout
, or domain-specific categories. These can be grouped under a common
folder:
components/
article/
List.vue
common/
Observer.vue
NoSSR.vue
layout/
AppFooter.vue
AppHeader.vue
ui/
AppButton.vue
AppList.vue
pages/
ArticlePage/
index.vue
ArticleTitle.vue
LastArticlesSection.vue
UserPage.vue
Adopting a clear Vue component folder structure improves your project's scalability and readability. By categorizing components into pages, UI, layout, domain, and common folders, you can maintain an organized and maintainable codebase. Experiment with these techniques and find a structure that suits your project's needs.
If you're looking to dive even deeper into organizing Vue applications and mastering scalable structures, don't miss our in-depth guide on How to Structure a Large-Scale Vue Application. It’s packed with actionable insights, practical examples, and best practices to take your Vue projects to the next level!
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.