Vue3(未完待续)

vue3项目结构(Vite 构建工具)

image-20250429173750357

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
my-vue3-project/
├── index.html # 入口 HTML 文件(必须位于根目录)
├── package.json # 项目依赖和脚本配置
├── vite.config.js # Vite 构建配置
├── tsconfig.json # TypeScript 配置(可选)
├── .gitignore # Git 忽略规则
├── public/ # 静态资源(不会被构建工具处理)
│ └── favicon.ico # 网站图标
└── src/ # 源代码目录
├── main.js/ts # 项目入口文件
├── App.vue # 根组件
├── assets/ # 图片、字体等静态资源
├── components/ # 可复用的 Vue 组件
├── views/ # 页面级组件(与路由对应)
├── router/ # Vue Router 配置
├── store/ # Pinia/Vuex 状态管理
├── api/ # API 请求封装
├── utils/ # 工具函数
├── styles/ # 全局样式文件
└── env.d.ts # TypeScript 环境声明

路由配置

1
npm install vue-router@4

src/router/index.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { createRouter, createWebHistory } from "vue-router"
import Home from "@/pages/Home.vue"
import About from "@/pages/About.vue"
import News from "@/pages/News.vue"

const router = createRouter({
history: createWebHistory(),
routes: [
{
path: "/home",
component: Home
},
{
path: "/about",
component: About
},
{
path: "/news",
component: News
}
]
})
export default router

src/main.ts

1
2
3
4
5
6
import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')

src/App.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<template>
<div class="app-container">
<h2 class="title">Welcome to Vue 3</h2>

<nav class="nav-bar">
<RouterLink
to="/home"
class="nav-link"
active-class="active">
首页
</RouterLink>
<RouterLink
to="/about"
class="nav-link"
active-class="active">
关于
</RouterLink>
<RouterLink
to="/news"
class="nav-link"
active-class="active">
新闻
</RouterLink>
</nav>

<main class="content-container">
<router-view></router-view>
</main>
</div>
</template>

<style scoped>
.app-container {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}

.title {
text-align: center;
font-size: 2.5rem;
color: #2c3e50;
margin-bottom: 2rem;
font-family: 'Arial Rounded MT Bold', sans-serif;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}

.nav-bar {
background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
padding: 1.2rem 2rem;
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
display: flex;
justify-content: center;
gap: 2rem;
}

.nav-link {
color: white;
text-decoration: none;
padding: 0.8rem 1.5rem;
border-radius: 8px;
transition: all 0.3s ease;
position: relative;
font-weight: 500;
display: flex;
align-items: center;
}

.nav-link:hover {
background: rgba(255, 255, 255, 0.15);
transform: translateY(-2px);
}

.nav-link.active {
background: white;
color: #6366f1;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.nav-link.active::after {
content: '';
position: absolute;
bottom: -8px;
left: 50%;
transform: translateX(-50%);
width: 60%;
height: 3px;
background: #6366f1;
border-radius: 2px;
}

.content-container {
background: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
min-height: 500px;
transition: all 0.3s ease;
}

/* 响应式设计 */
@media (max-width: 768px) {
.nav-bar {
flex-direction: column;
align-items: center;
gap: 1rem;
padding: 1rem;
}

.nav-link {
width: 100%;
justify-content: center;
}

.title {
font-size: 2rem;
}
}
</style>