Vue3 Project Description

A Vue 3 project consists of multiple files and folders, each with its specific purpose.

The following is a basic analysis of the Vue 3 project code to help you understand the project structure and the function of each part.

  • A Vue 3 project consists of multiple files and folders, and the core files includeindex.html、main.jsandApp.vue。

  • Vue components are the basic building blocks of an application, using Single-File Components (.vuefile) to define.

  • Vue Router is used for route management, and Vuex is used for state management.


Project Structure

A Vue 3 project typically contains the following files and folders:

my-vue-app/
├── node_modules/       # 项目依赖的第三方库
├── public/             # 静态资源文件夹
│   ├── index.html      # 应用的 HTML 模板
│   └── ...             # 其他静态资源(如图片、字体等)
├── src/                # 项目源代码
│   ├── assets/         # 静态资源(如图片、字体等)
│   ├── components/     # 可复用的 Vue 组件
│   ├── views/          # 页面级组件
│   ├── App.vue         # 根组件
│   ├── main.js         # 项目入口文件
│   ├── router.js       # 路由配置
│   ├── store.js        # Vuex 状态管理配置
│   └── ...             # 其他配置和资源
├── package.json        # 项目配置和依赖管理
├── package-lock.json   # 依赖的精确版本锁定文件
└── README.md           # 项目说明文档

Core File Analysis

public/index.html

public/index.htmlIt is the HTML template file of the Vue application.

Vue will mount the application to<div id="app"></div>inside.

Example

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue App</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

src/main.js

src/main.jsIt is the entry file of the Vue application.

src/main.jsIt is responsible for creating the Vue application instance and mounting the root component (usually App.vue) to the div#app in index.html.

Example

import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

src/App.vue

src/App.vueIt is the root component of the Vue application.

src/App.vueIt usually contains the main layout and route views of the application.

Example

<template>
  <div id="app">
    <h1>Hello, Vue 3!</h1>
    <router-view></router-view> <!-- Router view -->
  </div>
</template>

<script>
export default {
  name: 'App',
};
</script>

<style>
#app {
  text-align: center;
  margin-top: 50px;
}
</style>

src/components/

src/components/The folder contains reusable Vue components.

Example: HelloWorld.vue

<template>
  <div>
    <h2>Hello, World!</h2>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
};
</script>

<style scoped>
h2 {
  color: blue;
}
</style>

src/views/

src/views/The folder contains page-level components, usually used together with routing configuration.

Example: Home.vue

<template>
  <div>
    <h2>Home Page</h2>
  </div>
</template>

<script>
export default {
  name: 'Home',
};
</script>

src/router.js

src/router.jsIt is the Vue Router configuration file, used to define routes.

Example

import { createRouter, createWebHistory } from 'vue-router';
import Home from './views/Home.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

src/store.js

src/store.jsIt is the configuration file for Vuex state management (if Vuex is used).

Example

import { createStore } from 'vuex';

export default createStore({
  state: {
    message: 'Hello, Vuex!',
  },
  mutations: {
    setMessage(state, newMessage) {
      state.message = newMessage;
    },
  },
  actions: {
    updateMessage({ commit }, newMessage) {
      commit('setMessage', newMessage);
    },
  },
});

package.json

package.jsonIt is the project configuration file, containing project metadata, dependencies, and scripts.

Example

{
  "name": "my-vue-app",
  "version": "1.0.0",
  "scripts": {
    "serve": "vue-cli-service serve", // Start development server
    "build": "vue-cli-service build", // Build production code
    "lint": "vue-cli-service lint"    // Code formatting
  },
  "dependencies": {
    "vue": "^3.2.0",
    "vue-router": "^4.0.0",
    "vuex": "^4.0.0"
  }
}

Basic Structure of Vue Components

Vue components are the core building blocks of a Vue application.

A component usually contains the following parts:

1. Single-File Components (.vue files)

A.vuefile contains three parts:<template>、<script>and<style>。

Example

<template>
  <div>
    <h2>{{ message }}</h2>
    <button @click="updateMessage">Update Message</button>
  </div>
</template>

<script>
export default {
  name: 'MyComponent',
  data() {
    return {
      message: 'Hello, Vue!',
    };
  },
  methods: {
    updateMessage() {
this.message = 'Message updated!';
    },
  },
};
</script>

<style scoped>
h2 {
  color: green;
}
</style>

2. Template (<template>)

  • Defines the component's UI using HTML and Vue template syntax.

  • Supports interpolation ({{ }}), directives (such asv-if、v-for) and event binding (such as@click)。

3. Script (<script>)

  • Defines the component's logic, including data, methods, lifecycle hooks, etc.

  • Useexport defaultto export component options.

4. Style (<style>)

  • Defines the component's styles.

  • Usingscopedthe attribute can limit the styles to the current component.


Vue Project Execution Process

  1. Start the development server:

    • Runnpm run serveoryarn serveto start the development server.

    • Open the browser and visithttp://localhost:8080to view the application.

  2. Build production code:

    • Runnpm run buildoryarn buildto generate optimized production code.

  3. Code Formatting:

    • Runnpm run lintoryarn lintto format the code.

Other Extensions