Vue3 Project Intro
Vue 3 projects consist of multiple files and folders, each serving a specific purpose.
The following is a basic analysis of Vue 3 project code to help you understand the project structure and the function of each part.
* Vue 3 projects consist of multiple files and folders, with core files including `index.html`, `main.js`, and `App.vue`.
* Vue components are the basic building blocks of applications, defined using Single File Components (`.vue` files).
* Vue Router is used for routing 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/ # Third-party libraries the project depends onβββ public/ # Static resources folderβ βββ index.html # HTML template for the applicationβ βββ ... # Other static resources (such as images, fonts, etc.)βββ src/ # Project source codeβ βββ assets/ # Static resources (such as images, fonts, etc.)β βββ components/ # Reusable Vue componentsβ βββ views/ # Page-level componentsβ βββ App.vue # Root componentβ βββ main.js # Project entry fileβ βββ router.js # Routing configurationβ βββ store.js # Vuex state management configurationβ βββ ... # Other configurations and resourcesβββ package.json # Project configuration and dependency managementβββ package-lock.json # Exact version lock file for dependenciesβββ README.md # Project documentation
* * *
## Core File Analysis
### public/index.html
**public/index.html** is the HTML template file for the Vue application.
Vue will mount the application into ``.
## Example
Vue App
### src/main.js
**src/main.js** is the entry file for the Vue application.
**src/main.js** 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.vue** is the root component of the Vue application.
**src/App.vue** typically contains the main layout and routing views of the application.
## 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 routing configuration.
## Example: Home.vue
### src/router.js
**src/router.js** is the configuration file for Vue Router, 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 configuration file for Vuex state management (if using Vuex).
## 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 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 environment 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 Vue applications.
A component typically contains the following parts:
### 1. Single File Components (.vue files)
A `.vue` file contains three parts: ``, `
### 2. Template (``)
* Define the component's UI using HTML and Vue template syntax.
* Supports interpolation (`{{ }}`), directives (such as `v-if`, `v-for`), and event binding (such as `@click`).
### 3. Script (`
YouTip