What is the difference between the views and components folders in a Vue project?

vue.jsVuejs2Vue CliVue Cli-3Vue Cli-4

vue.js Problem Overview


I just used the command line (CLI) to initialize a Vue.js project. The CLI created a src/components and src/views folder.

It has been a few months since I have worked with a Vue project and the folder structure seems new to me.

What is the difference between the views and components folders in a Vue project generated with vue-cli?

vue.js Solutions


Solution 1 - vue.js

First of all, both folders, src/components and src/views, contain Vue components.

The key difference is that some Vue components act as Views for routing.

When dealing with routing in Vue, usually with Vue Router, routes are defined in order to switch the current view used in the <router-view> component. These routes are typically located at src/router/routes.js, where we can see something like this:

import Home from '@/views/Home.vue'
import About from '@/views/About.vue'

export default [
  {
    path: '/',
    name: 'home',
    component: Home,
  },
  {
    path: '/about',
    name: 'about',
    component: About,
  },
]

The components located under src/components are less likely to be used in a route whereas components located under src/views will be used by at least one route.


> Vue CLI aims to be the standard tooling baseline for the Vue > ecosystem. It ensures the various build tools work smoothly together > with sensible defaults so you can focus on writing your app instead of > spending days wrangling with configurations. At the same time, it > still offers the flexibility to tweak the config of each tool without > the need for ejecting.

Vue CLI aims for rapid Vue.js development, it keeps things simple and offers flexibility. Its goal is to enable teams of varying skill levels to set up a new project and get started.

At the end of the day, it is a matter of convenience and application structure.

  • Some people like to have their Views folder under src/router like this enterprise boilerplate.
  • Some people call it Pages instead of Views.
  • Some people have all their components under the same folder.

Choose the application structure that best suits the project you are working on.

Solution 2 - vue.js

I think its more of a convention. Something that is reusable can be kept in src/components folder something that is tied to router can be kept in src/views

Solution 3 - vue.js

Generally re-usable views are suggested to be placed in src/components directory. Examples like Header, Footer, Ads, Grids or any custom controls likes styled text boxes or buttons. One or more components can be accessed inside a view.

A View can have component(s) and a view is actually intended to be accessed by navigation url. They are generally placed in src/views.

Remember that you are not constrained to access the Components via url. You are free to add any component to the router.js and access it too. But if you are intended to do it so, you can move it to a src/views rather than placing it in src/components.

Components are user-controls in analogy to asp.net web forms.

Its just about structuring your code for better maintenance and readability.

Solution 4 - vue.js

Both folders are basically the same since they both hold components, but the aesthetic of Vue is that components that will function as pages (routed to like page for navigation) are kept in the /views folder, while reusable components like form fields are kept in the /components folder.

Solution 5 - vue.js

src/views is typically used for your main pages in the application that you navigate via router. src/components is used for the reusable components that you use inside your main pages (multiple times inside the same page or across different pages)

Solution 6 - vue.js

Simple, Views are for routes while Components are components of the route.

Solution 7 - vue.js

You can consider Views like page and components are reusable block of code that you can use in any page or components (both are Vue files these terms are just for demonstration)

Solution 8 - vue.js

Less dynamic close to static pages is reffered to views and more reuseable and dynamic content is placed under the components.

Solution 9 - vue.js

It is quite simple, as mentioned by others: you usually use Views for the actual pages you want the user to navigate. Components are the elements inside those pages that you can reuse in any page of your project.

Solution 10 - vue.js

Both these folders hold Vue components, 'views' folder is supposed to contain root level components where other components would be imported. The so called 'other components' reside inside the 'components' folder. Let's take an example for illustration.

Suppose you have 3 root level pages for a website yourname.com

  • yourname.com
  • yourname.com/about
  • yourname.com/price

Your 'views' folder would have 3 components. 'about.vue', 'index.vue' and 'price.vue'. These files would be imported in your router file or could be directly imported in app.vue file for routing. These views could have multiple components inside them like 'price-card.vue', 'contact-card.vue' and more. These files would typically reside inside a folder named 'components'. You can import these components inside the vue files you have in the 'views' folder and then render them.

Solution 11 - vue.js

In my view, component folder must contain the components that are going to be used in the views. And in views, there must be those pages that are to be accessed by the router. For example, you have a navbar, header and a footer in your pages to be used and you have a login page, signup page and a main page. Then your src/components must contain header, footer and navbar. And in your src/views there must be files like login, signup and main file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondrsnarkView Question on Stackoverflow
Solution 1 - vue.jsRickyView Answer on Stackoverflow
Solution 2 - vue.jsRaviView Answer on Stackoverflow
Solution 3 - vue.jsnavuleView Answer on Stackoverflow
Solution 4 - vue.jsChinedu ChazView Answer on Stackoverflow
Solution 5 - vue.jsUdara SandeshaView Answer on Stackoverflow
Solution 6 - vue.jsJan MichaelView Answer on Stackoverflow
Solution 7 - vue.jsZaheer AlviView Answer on Stackoverflow
Solution 8 - vue.jsMuhammadView Answer on Stackoverflow
Solution 9 - vue.jsEduardo Pedra de OliveiraView Answer on Stackoverflow
Solution 10 - vue.jsAPFireboltView Answer on Stackoverflow
Solution 11 - vue.jsmadcoderView Answer on Stackoverflow