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.
-
A Vue 3 project consists of multiple files and folders; the core files include
index.html,main.jsandApp.vue. -
Vue components are the basic building blocks of an application, defined using Single File Components (
.vuefiles). -
Vue Router is used for routing, and Vuex 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.html is the HTML template file of the Vue application.
Vue mounts the application to it.
Example
Vue App
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
Hello, Vue 3!
src/components/
src/components/ folder contains reusable Vue components.
Example: HelloWorld.vue
Hello, World!
src/views/
src/views/ folder contains page-level components, usually used together with route configuration.
Example: Home.vue
Home Page
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:, and 。
Example
{{ message }}
Update message
2. Template ()
-
Uses HTML and Vue template syntax to define the component's UI.
-
Supports interpolation (
{{ }}), directives (such asv-if,v-for), and event binding (such as@click).
3. Script ()
-
Defines the component's logic, including data, methods, and lifecycle hooks.
-
Use
export defaultExports the component options.
4. Style ()
-
Defines the component's styles.
-
Use
scopedattribute can scope styles to the current component.
Vue Project Runtime Flow
-
Start the Dev Server:
Run npm run serve or yarn serve, to start the dev server.
Open the browser and visit http://localhost:8080, to view the application.
Run npm run serve or yarn serve, to start the dev server.
Open the browser and visit http://localhost:8080, to view the application.
-
Build Production Code:
Run npm run build or yarn build, to generate optimized production code.
Run npm run build or yarn build, to generate optimized production code.
-
Code Formatting:
Run npm run lint or yarn lint, to format the code.
Run npm run lint or yarn lint, to format the code.