A great way to structure and bootstrap VueJS + Vuetify + API projects
There is no magic formula to create a perfect VueJS project, but definitely there are "best practices" that will make a better development workflow
A common question when we start a project is to decide where to put each software piece, how to organize the project's structure, which linter, code style, library to handle environment variable and so on.
I'd like to present to you a well structured VueJS project that focus on productivity, maintainability and perhaps a more intuitive one. In this way, rather than creating a huge discussion among all developers to decide where things take place, it has a great initial structure. Indeed, it promotes your team to focus on the business or the domain's problem/solution first.
DISCLAIMER: It's always a good idea, start from scratch and add each library or code as soon it is required, understand why each piece is necessary is extremely important, however, doing this all the time could be waste of time.
π Note: The links for more details, references and the source code are in the end.
WHY
- Have a great initial structure and delivery the first feature faster
- Have an organization that helps you have context separated in different files/folders
Main Features
- Page inheritance
- Routes
- API Ready + API Mock
- Linter + Code Style
- Working examples
- Easy to rename, change or even remove things
- dotenv
Show me the code
Vue initial structure
Many of you might be familiar with the initial vue create my-project
structure:
# vue create my-project
my-project
βββ babel.config.js
βββ package.json
βββ public
β βββ favicon.ico
β βββ index.html
βββ README.md
βββ src
βββ App.vue
βββ assets
β βββ logo.png
βββ components
β βββ HelloWorld.vue
βββ main.js
Even though they keep things simple (KISS) which is great! Most of the time, we need to add a lot of things. The key here is, save time and avoid unnecessary discussions.
If you need a how to step by step for an initial VueJS project, I recommend this post here .
A better project structure
Below is the initial structure generate by this vue project template . Don't be scared, it's simpler than it seems and it is lean and few dependencies.
# vue init huogerac/crud-vuetify-structured-template my-project
my-project
βββ apiMock
β βββ db.json
β βββ server
β βββ config.js
β βββ index.js
βββ babel.config.js
βββ package.json
βββ .prettierrc.js
βββ .env
βββ public
β βββ favicon.ico
β βββ index.html
βββ src
β βββ api
β β βββ index.js
β β βββ tasks.api.js
β βββ App.vue
β βββ assets
β β βββ 404_notfound.svg
β β βββ 500_server_down.png
β βββ components
β β βββ TaskCreateDialog.vue
β β βββ TaskUpdateDialog.vue
β β βββ AppBar.vue
β β βββ Footer.vue
β β βββ TaskCard.vue
β β βββ TaskForm.vue
β βββ filters
β β βββ dateFilter.js
β βββ main.js
β βββ mixins
β β βββ ApiResponseMixin.vue
β βββ pages
β β βββ layouts
β β β βββ Public.vue
β β βββ public
β β βββ 404.vue
β β βββ 500.vue
β β βββ Home.vue
β βββ plugins
β β βββ vuetify.js
β βββ router
β β βββ index.js
β β βββ public.routes.js
β βββ settings.js
βββ vue.config.js
Let's make it simpler to understand:
First we have the flow, from the url which can be the root /
or some specific page, such as /about-me
that is the entry point.
Basically, we have Route [1] that matches the URL and is related to one Page [2]. Pages are Vue Components too, however, they have a specific folder (pages
)
Then we have Pages X Components X Pure Components:
Everything are Vue Components too, Let's start with the Pure Components [3] (or DUMP), the key here is they are 100% visual, in another words, there is no call for external things or the API. Pure components use this.$emit
to send events to others components.
Then we have the "non Pure Components", they are the SMART components where we make rules and calls to the API. The idea is keep the SMART components inside the src/pages
Here a little bit more details about each folder:
βββ src
βββ router π #1 App routes
β βββ index.js
β βββ tasks.api.js π Routers by context
βββ pages π #2 App pages/views ("SMART")
β βββ layouts π Pages bases
β β βββ Public.vue
β βββ public π Pages by context
β βββ 500.vue
β βββ Home.vue
βββ components π #3 VueJS components files ("DUMB")
βββ api π #4 API folder
β βββ index.js
β βββ tasks.api.js π API endpoints by context
βββ mixins π Anything used cross components
β βββ ApiResponse.vue
βββ filters π VueJS template filters
β βββ dateFilter.js
βββ assets
β βββ some-image.jpg
βββ plugins
β βββ vuetify.js
βββ package.json
βββ .prettierrc.js
There is also the apiMock
folder which contains the API Mock and make your life easier without be dependent of a real API Backend (other NodeJS or Python application). Checkout the npm run api:mock
defined inside the package.json
, it starts the API Mock over the port 3001 and make available all data you add inside the apiMock/server/db.json
available as API. Thanks the awesome json-server library.
Naming components
An idea is to use:
- App or Base prefix for generic components (e.g. AppBar)
- The prefix for one usage components (e.g. TheFooter)
- "Something" prefix for group by context (e.g. UserResetPassword, TaskForm)
What's next
- Checkout the repository crud-vuetify-structured-template for more details, run locally and see it running just with few commands
- Contribute giving ideas and feedback, let's make this structure even better! Use the hashnode comments or directly the repository issues
I hope you've enjoyed this article. Special thanks to my friend Luiz OtΓ‘vio who helped tremendously in this structure.