Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Project Overview

Vue 3 Project Overview

A Vue 3 project consists of multiple files and folders, each with a specific role.

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


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.html is the HTML template file of the Vue application.

Vue mounts the application to

it.

Example

 

   

   

   

<p>Vue App</p>

 

 

   

 

src/main.js

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

src/main.js Responsible for creating the Vue application instance and mounting the root component (usually App.vue) into 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.vue is the root component of the Vue application.

src/App.vue Usually contains the application's main layout and router view.

Example

src/components/

src/components/ folder contains reusable Vue components.

Example: HelloWorld.vue

src/views/

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

Example: Home.vue

src/router.js

src/router.js 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.js is the Vuex state management configuration file (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.json is the project's configuration file, containing the project's metadata, dependencies, and scripts.

Example

{

 

"name"

:

"my-vue-app"

,

 

"version"

:

"1.0.0"

,

 

"scripts"

:

{

   

"serve"

:

"vue-cli-service serve"

,

// start the dev server

   

"build"

:

"vue-cli-service build"

,

// build production code

   

"lint"

:

"vue-cli-service lint"

   

// format code

 

}

,

 

"dependencies"

:

{

   

"vue"

:

"^3.2.0"

,

   

"vue-router"

:

"^4.0.0"

,

   

"vuex"

:

"^4.0.0"

 

}

}


Basic Structure of a Vue Component

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

A component usually contains the following parts:

1. Single File Component (.vue file)

A .vue .vue file contains three parts: