vue.jsvuejs2vue-clivue-cli-3vue-cli-4

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


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?


Solution

  • 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.


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