create-vite-vue 1.5.5 → 1.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -96
- package/bin/index.js +9 -15
- package/package.json +1 -1
- package/template/base-js/README.md +152 -96
- package/template/base-js/package.json.tpl +3 -6
- package/template/base-ts/README.md +152 -96
- package/template/base-ts/package.json.tpl +6 -9
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
- 🌐 本地及网络访问启动日志显示
|
|
20
20
|
- 📝 自定义 Banner 插件显示项目信息
|
|
21
21
|
- 🎨 可选集成 Tailwind CSS(通过 postcss 配置)
|
|
22
|
+
- 🟢 支持多种包管理器:npm / pnpm,可根据环境自动识别并使用
|
|
23
|
+
- 🔔 可选集成 Mitt(轻量事件总线,实现组件间解耦通信)
|
|
22
24
|
|
|
23
25
|
---
|
|
24
26
|
|
|
@@ -33,6 +35,7 @@
|
|
|
33
35
|
🖥️ Element Plus / 📱 Vant
|
|
34
36
|
🧰 VueUse · Lodash · Day.js
|
|
35
37
|
🎨 Tailwind CSS
|
|
38
|
+
🔔 Mitt
|
|
36
39
|
|
|
37
40
|
---
|
|
38
41
|
|
|
@@ -58,126 +61,97 @@
|
|
|
58
61
|
|
|
59
62
|
## 📁 项目目录结构说明
|
|
60
63
|
|
|
61
|
-
project-name
|
|
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
|
-
|
|
64
|
+
> project-name
|
|
65
|
+
>
|
|
66
|
+
> ├─ public/ —— 公共静态资源目录
|
|
67
|
+
>
|
|
68
|
+
> │ └─ favicon.ico
|
|
69
|
+
>
|
|
70
|
+
> ├─ src/
|
|
71
|
+
>
|
|
72
|
+
> │ ├─ api/ —— 接口请求封装目录
|
|
73
|
+
>
|
|
74
|
+
> │ ├─ assets/ —— 图片、字体等静态资源
|
|
75
|
+
>
|
|
76
|
+
> │ ├─ components/ —— 公共组件目录
|
|
77
|
+
>
|
|
78
|
+
> │ ├─ router/ —— 路由配置(可选)
|
|
79
|
+
>
|
|
80
|
+
> │ ├─ stores/ —— Pinia 状态管理(可选)
|
|
81
|
+
>
|
|
82
|
+
> │ ├─ types/ —— 类型声明文件
|
|
83
|
+
>
|
|
84
|
+
> │ ├─ utils/ —— 工具方法、请求封装
|
|
85
|
+
>
|
|
86
|
+
> │ ├─ views/ —— 页面级组件(路由页面)
|
|
87
|
+
>
|
|
88
|
+
> │ │ └─ home/ —— 示例页面文件夹
|
|
89
|
+
>
|
|
90
|
+
> │ │ ├─ index.vue —— 默认首页 /home
|
|
91
|
+
>
|
|
92
|
+
> │ │ ├─ meta.json —— 页面 meta 信息
|
|
93
|
+
>
|
|
94
|
+
> │ │ └─ [id]/ —— 动态参数路由示例
|
|
95
|
+
>
|
|
96
|
+
> │ │ └─ [name].vue —— 路由 /home/:id/:name
|
|
97
|
+
>
|
|
98
|
+
> │ ├─ App.vue —— 根组件
|
|
99
|
+
>
|
|
100
|
+
> │ ├─ main.js / main.ts —— 项目启动入口
|
|
101
|
+
>
|
|
102
|
+
> │ └─ style.css —— 全局样式文件
|
|
103
|
+
>
|
|
104
|
+
> ├─ index.html —— 项目入口页面
|
|
105
|
+
>
|
|
106
|
+
> ├─ jsconfig.json / tsconfig.json —— 路径别名与编译配置
|
|
107
|
+
>
|
|
108
|
+
> ├─ package.json —— 项目依赖与脚本配置
|
|
109
|
+
>
|
|
110
|
+
> ├─ postcss.config.mjs —— Tailwind CSS 配置文件(可选)
|
|
111
|
+
>
|
|
112
|
+
> ├─ README.md —— 项目说明文档
|
|
113
|
+
>
|
|
114
|
+
> └─ vite.config.ts —— Vite 开发与构建配置
|
|
101
115
|
|
|
102
116
|
---
|
|
103
117
|
|
|
104
|
-
## ⚙️
|
|
118
|
+
## ⚙️ 使用方式(多包管理器支持)
|
|
105
119
|
|
|
106
120
|
1. 创建项目
|
|
121
|
+
|
|
107
122
|
```bash
|
|
108
123
|
npm create vite-vue@latest
|
|
124
|
+
pnpm create vite-vue@latest
|
|
109
125
|
```
|
|
110
126
|
|
|
111
127
|
2. 进入项目目录
|
|
128
|
+
|
|
112
129
|
```bash
|
|
113
130
|
cd 项目名
|
|
114
131
|
```
|
|
115
132
|
|
|
116
|
-
3. 安装依赖
|
|
133
|
+
3. 安装依赖
|
|
134
|
+
|
|
117
135
|
```bash
|
|
118
136
|
npm install
|
|
137
|
+
pnpm install
|
|
119
138
|
```
|
|
120
139
|
|
|
121
140
|
4. 启动开发环境
|
|
141
|
+
|
|
122
142
|
```bash
|
|
123
143
|
npm run dev
|
|
144
|
+
pnpm dev
|
|
124
145
|
```
|
|
125
146
|
|
|
126
|
-
> 如果在创建时选择了「立即运行」,工具会自动执行启动命令。
|
|
127
|
-
|
|
128
|
-
---
|
|
129
|
-
|
|
130
|
-
## 🌐 技术栈官网链接
|
|
131
|
-
|
|
132
|
-
- [Vite](https://vitejs.dev/)
|
|
133
|
-
- [Vue 3](https://vuejs.org/)
|
|
134
|
-
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
135
|
-
- [TypeScript](https://www.typescriptlang.org/)
|
|
136
|
-
- [Vue Router](https://router.vuejs.org/)
|
|
137
|
-
- [Pinia](https://pinia.vuejs.org/)
|
|
138
|
-
- [Axios](https://axios-http.com/)
|
|
139
|
-
- [Element Plus](https://element-plus.org/)
|
|
140
|
-
- [Vant](https://vant-contrib.gitee.io/vant/)
|
|
141
|
-
- [VueUse](https://vueuse.org/)
|
|
142
|
-
- [Lodash](https://lodash.com/)
|
|
143
|
-
- [Day.js](https://day.js.org/)
|
|
144
|
-
- [Tailwind CSS](https://tailwindcss.com/)
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
|
-
## 🟢 Node.js 版本管理(.nvmrc)
|
|
149
|
-
|
|
150
|
-
项目根目录中包含一个 **`.nvmrc`** 文件,用于指定当前项目推荐使用的**Node.js 版本**。这样可以保证不同开发者或不同机器运行项目时使用一致的 Node环境,避免依赖问题。
|
|
151
|
-
|
|
152
|
-
### 使用 nvm 切换 Node 版本
|
|
153
|
-
|
|
154
|
-
如果你安装了 **nvm(Node Version Manager)**,进入项目目录后执行:
|
|
155
|
-
|
|
156
|
-
``` bash
|
|
157
|
-
nvm use
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
nvm 会自动读取 `.nvmrc` 文件中的 Node 版本并切换。
|
|
161
|
-
|
|
162
|
-
### Windows 可能出现的问题
|
|
163
|
-
|
|
164
|
-
在 **Windows PowerShell** 中,`nvm use` 有时不会自动读取`.nvmrc`,可能会报错或无法切换版本。
|
|
165
|
-
|
|
166
|
-
如果出现这种情况,可以执行:
|
|
167
|
-
|
|
168
|
-
``` powershell
|
|
169
|
-
nvm use $(Get-Content .nvmrc)
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
该命令会读取 `.nvmrc` 文件内容并手动切换 Node 版本。
|
|
173
|
-
|
|
174
147
|
---
|
|
175
148
|
|
|
176
149
|
## 🔧 常见需要调整的地方(具体文件示例)
|
|
177
150
|
|
|
178
151
|
### 1️⃣ 接口请求地址
|
|
179
152
|
|
|
180
|
-
文件:`src/utils/request.ts`
|
|
153
|
+
文件:`src/utils/request.ts` / `src/utils/request.js`
|
|
154
|
+
|
|
181
155
|
```ts
|
|
182
156
|
import axios from 'axios';
|
|
183
157
|
|
|
@@ -187,7 +161,7 @@ const service = axios.create({
|
|
|
187
161
|
});
|
|
188
162
|
|
|
189
163
|
// 示例请求
|
|
190
|
-
export const getUserList = () =>
|
|
164
|
+
export const getUserList = () => service.get('/users');
|
|
191
165
|
|
|
192
166
|
export default service;
|
|
193
167
|
```
|
|
@@ -198,7 +172,8 @@ export default service;
|
|
|
198
172
|
|
|
199
173
|
### 2️⃣ 本地代理配置
|
|
200
174
|
|
|
201
|
-
文件:`vite.config.ts`
|
|
175
|
+
文件:`vite.config.ts` / `vite.config.js`
|
|
176
|
+
|
|
202
177
|
```ts
|
|
203
178
|
import { defineConfig } from 'vite';
|
|
204
179
|
import vue from '@vitejs/plugin-vue';
|
|
@@ -224,7 +199,8 @@ export default defineConfig({
|
|
|
224
199
|
|
|
225
200
|
### 3️⃣ 路由结构(支持动态参数)
|
|
226
201
|
|
|
227
|
-
文件:`src/router/index.ts`
|
|
202
|
+
文件:`src/router/index.ts` / `src/router/index.js`
|
|
203
|
+
|
|
228
204
|
```ts
|
|
229
205
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
230
206
|
import routes from '~pages' // 自动生成的路由
|
|
@@ -237,14 +213,16 @@ const router = createRouter({
|
|
|
237
213
|
export default router
|
|
238
214
|
```
|
|
239
215
|
|
|
240
|
-
>
|
|
216
|
+
> 页面文件夹结构示例:
|
|
217
|
+
|
|
241
218
|
```text
|
|
242
219
|
src/views/home/
|
|
243
220
|
├─ index.vue -> /home
|
|
244
221
|
└─ [id]/[name].vue -> /home/:id/:name
|
|
245
222
|
```
|
|
246
223
|
|
|
247
|
-
>
|
|
224
|
+
> 获取路由参数:
|
|
225
|
+
|
|
248
226
|
```ts
|
|
249
227
|
import { useRoute } from 'vue-router'
|
|
250
228
|
const route = useRoute()
|
|
@@ -257,6 +235,7 @@ console.log(route.params.name)
|
|
|
257
235
|
### 4️⃣ 页面内容与样式
|
|
258
236
|
|
|
259
237
|
文件示例:`src/views/home/index.vue`
|
|
238
|
+
|
|
260
239
|
```vue
|
|
261
240
|
<template>
|
|
262
241
|
<div class="home-container">
|
|
@@ -281,6 +260,64 @@ console.log(route.params.name)
|
|
|
281
260
|
|
|
282
261
|
---
|
|
283
262
|
|
|
263
|
+
### 5️⃣ Mitt 事件总线使用示例
|
|
264
|
+
|
|
265
|
+
#### 封装事件总线
|
|
266
|
+
|
|
267
|
+
文件:`src/utils/eventBus.ts`
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
import mitt from 'mitt'
|
|
271
|
+
|
|
272
|
+
type Events = {
|
|
273
|
+
userLogin: { name: string },
|
|
274
|
+
userLogout: null
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const emitter = mitt<Events>()
|
|
278
|
+
|
|
279
|
+
export default emitter
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### 使用示例
|
|
283
|
+
|
|
284
|
+
##### 组件 A(发送事件)
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
<script setup lang="ts">
|
|
288
|
+
import eventBus from '@/utils/eventBus'
|
|
289
|
+
|
|
290
|
+
function login() {
|
|
291
|
+
eventBus.emit('userLogin', { name: '张三' })
|
|
292
|
+
}
|
|
293
|
+
</script>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
##### 组件 B(接收事件)
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
<script setup lang="ts">
|
|
300
|
+
import { onMounted, onUnmounted } from 'vue'
|
|
301
|
+
import eventBus from '@/utils/eventBus'
|
|
302
|
+
|
|
303
|
+
function handleLogin(payload: { name: string }) {
|
|
304
|
+
console.log('用户登录:', payload.name)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
onMounted(() => {
|
|
308
|
+
eventBus.on('userLogin', handleLogin)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
onUnmounted(() => {
|
|
312
|
+
eventBus.off('userLogin', handleLogin)
|
|
313
|
+
})
|
|
314
|
+
</script>
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
> 🔹 Mitt 事件总线轻量高效,适合组件间解耦通信,替代父子传递 props / emit 或 Vuex/Pinia 中的小型事件场景。
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
284
321
|
## 🎯 适用场景
|
|
285
322
|
|
|
286
323
|
- Vue 3 新手快速上手
|
|
@@ -291,6 +328,25 @@ console.log(route.params.name)
|
|
|
291
328
|
|
|
292
329
|
---
|
|
293
330
|
|
|
331
|
+
## 🌐 技术栈官网链接
|
|
332
|
+
|
|
333
|
+
- [Vite | 下一代的前端工具链](https://cn.vitejs.dev/)
|
|
334
|
+
- [Vue.js - 渐进式 JavaScript 框架 | Vue.js](https://cn.vuejs.org/)
|
|
335
|
+
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
336
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
337
|
+
- [Vue Router | Vue.js 的官方路由](https://router.vuejs.org/zh/)
|
|
338
|
+
- [Pinia | The intuitive store for Vue.js](https://pinia.vuejs.org/zh/)
|
|
339
|
+
- [Axios](https://www.axios-http.cn/)
|
|
340
|
+
- [Element Plus | 一个 Vue 3 UI 框架](https://element-plus.org/zh-CN/)
|
|
341
|
+
- [Vant 4 - 轻量、可定制的移动端组件库](https://vant-ui.github.io/vant/#/zh-CN)
|
|
342
|
+
- [VueUse](https://vueuse.pages.dev/)
|
|
343
|
+
- [Lodash](https://www.lodashjs.com/)
|
|
344
|
+
- [Day.js](https://day.js.org/)
|
|
345
|
+
- [Tailwind CSS](https://tailwindcss.com/)
|
|
346
|
+
- [Mitt](https://github.com/developit/mitt)
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
294
350
|
## 📄 License
|
|
295
351
|
|
|
296
352
|
MIT License
|
package/bin/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
import { execSync } from 'child_process'
|
|
3
3
|
import fs from 'fs'
|
|
4
4
|
import path from 'path'
|
|
@@ -41,10 +41,6 @@ const pkgCommands = {
|
|
|
41
41
|
install: 'npm install',
|
|
42
42
|
dev: 'npm run dev'
|
|
43
43
|
},
|
|
44
|
-
yarn: {
|
|
45
|
-
install: 'yarn',
|
|
46
|
-
dev: 'yarn dev'
|
|
47
|
-
},
|
|
48
44
|
pnpm: {
|
|
49
45
|
install: 'pnpm install',
|
|
50
46
|
dev: 'pnpm dev'
|
|
@@ -104,7 +100,8 @@ const pkgCommands = {
|
|
|
104
100
|
{ title: 'VueUse(实用 Composition API)', value: 'vueuse' },
|
|
105
101
|
{ title: 'Lodash(工具库)', value: 'lodash' },
|
|
106
102
|
{ title: 'Day.js(日期处理)', value: 'dayjs' },
|
|
107
|
-
{ title: 'Tailwind CSS(原子化 CSS)', value: 'tailwind' }
|
|
103
|
+
{ title: 'Tailwind CSS(原子化 CSS)', value: 'tailwind' },
|
|
104
|
+
{ title: 'mitt(事件总线)', value: 'mitt' }
|
|
108
105
|
]
|
|
109
106
|
})
|
|
110
107
|
|
|
@@ -117,7 +114,7 @@ const pkgCommands = {
|
|
|
117
114
|
}
|
|
118
115
|
|
|
119
116
|
const extraPlugins = featureList?.filter(v =>
|
|
120
|
-
['vueuse', 'lodash', 'dayjs', 'tailwind'].includes(v)
|
|
117
|
+
['vueuse', 'lodash', 'dayjs', 'tailwind', 'mitt'].includes(v)
|
|
121
118
|
) || []
|
|
122
119
|
|
|
123
120
|
// 询问是否开启自动路由
|
|
@@ -253,7 +250,7 @@ for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
|
|
253
250
|
}
|
|
254
251
|
if(features.axios) optionalDeps['axios'] = '^1.13.6'
|
|
255
252
|
if(features.ui.includes('element')) {
|
|
256
|
-
optionalDeps['element-plus'] = '^2.13.
|
|
253
|
+
optionalDeps['element-plus'] = '^2.13.5'
|
|
257
254
|
optionalDeps['@element-plus/icons-vue'] = '^2.3.2'
|
|
258
255
|
}
|
|
259
256
|
if(features.ui.includes('vant')) {
|
|
@@ -261,13 +258,14 @@ for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
|
|
261
258
|
}
|
|
262
259
|
// 增强插件依赖
|
|
263
260
|
if(extraPlugins.includes('vueuse')) optionalDeps['@vueuse/core'] = '^14.2.1'
|
|
264
|
-
if(extraPlugins.includes('dayjs')) optionalDeps['dayjs'] = '^1.11.
|
|
261
|
+
if(extraPlugins.includes('dayjs')) optionalDeps['dayjs'] = '^1.11.20'
|
|
265
262
|
if(extraPlugins.includes('lodash')) optionalDeps['lodash'] = '^4.17.23'
|
|
266
263
|
if(extraPlugins.includes('tailwind')) {
|
|
267
|
-
optionalDeps['tailwindcss'] = '^4.2.
|
|
268
|
-
optionalDeps['@tailwindcss/postcss'] = '^4.2.
|
|
264
|
+
optionalDeps['tailwindcss'] = '^4.2.2'
|
|
265
|
+
optionalDeps['@tailwindcss/postcss'] = '^4.2.2'
|
|
269
266
|
optionalDeps['postcss'] = '^8.5.8'
|
|
270
267
|
}
|
|
268
|
+
if(extraPlugins.includes('mitt')) optionalDeps['mitt'] = '^3.0.1'
|
|
271
269
|
if(autoRoute) optionalDeps['vite-plugin-pages'] = '^0.33.3'
|
|
272
270
|
|
|
273
271
|
let depsStr = ''
|
|
@@ -391,8 +389,6 @@ export default createRouter({
|
|
|
391
389
|
|
|
392
390
|
if(pkgManager === 'pnpm') {
|
|
393
391
|
installCmd = 'pnpm install'
|
|
394
|
-
} else if(pkgManager === 'yarn') {
|
|
395
|
-
installCmd = 'yarn'
|
|
396
392
|
} else {
|
|
397
393
|
installCmd = 'npm install'
|
|
398
394
|
}
|
|
@@ -417,11 +413,9 @@ function detectPackageManager () {
|
|
|
417
413
|
const userAgent = process.env.npm_config_user_agent || ''
|
|
418
414
|
|
|
419
415
|
if(userAgent.startsWith('pnpm')) return 'pnpm'
|
|
420
|
-
if(userAgent.startsWith('yarn')) return 'yarn'
|
|
421
416
|
if(userAgent.startsWith('npm')) return 'npm'
|
|
422
417
|
|
|
423
418
|
if(fs.existsSync('pnpm-lock.yaml')) return 'pnpm'
|
|
424
|
-
if(fs.existsSync('yarn.lock')) return 'yarn'
|
|
425
419
|
|
|
426
420
|
return 'npm'
|
|
427
421
|
}
|
package/package.json
CHANGED
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
- 🌐 本地及网络访问启动日志显示
|
|
20
20
|
- 📝 自定义 Banner 插件显示项目信息
|
|
21
21
|
- 🎨 可选集成 Tailwind CSS(通过 postcss 配置)
|
|
22
|
+
- 🟢 支持多种包管理器:npm / pnpm,可根据环境自动识别并使用
|
|
23
|
+
- 🔔 可选集成 Mitt(轻量事件总线,实现组件间解耦通信)
|
|
22
24
|
|
|
23
25
|
---
|
|
24
26
|
|
|
@@ -33,6 +35,7 @@
|
|
|
33
35
|
🖥️ Element Plus / 📱 Vant
|
|
34
36
|
🧰 VueUse · Lodash · Day.js
|
|
35
37
|
🎨 Tailwind CSS
|
|
38
|
+
🔔 Mitt
|
|
36
39
|
|
|
37
40
|
---
|
|
38
41
|
|
|
@@ -58,126 +61,97 @@
|
|
|
58
61
|
|
|
59
62
|
## 📁 项目目录结构说明
|
|
60
63
|
|
|
61
|
-
project-name
|
|
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
|
-
|
|
64
|
+
> project-name
|
|
65
|
+
>
|
|
66
|
+
> ├─ public/ —— 公共静态资源目录
|
|
67
|
+
>
|
|
68
|
+
> │ └─ favicon.ico
|
|
69
|
+
>
|
|
70
|
+
> ├─ src/
|
|
71
|
+
>
|
|
72
|
+
> │ ├─ api/ —— 接口请求封装目录
|
|
73
|
+
>
|
|
74
|
+
> │ ├─ assets/ —— 图片、字体等静态资源
|
|
75
|
+
>
|
|
76
|
+
> │ ├─ components/ —— 公共组件目录
|
|
77
|
+
>
|
|
78
|
+
> │ ├─ router/ —— 路由配置(可选)
|
|
79
|
+
>
|
|
80
|
+
> │ ├─ stores/ —— Pinia 状态管理(可选)
|
|
81
|
+
>
|
|
82
|
+
> │ ├─ types/ —— 类型声明文件
|
|
83
|
+
>
|
|
84
|
+
> │ ├─ utils/ —— 工具方法、请求封装
|
|
85
|
+
>
|
|
86
|
+
> │ ├─ views/ —— 页面级组件(路由页面)
|
|
87
|
+
>
|
|
88
|
+
> │ │ └─ home/ —— 示例页面文件夹
|
|
89
|
+
>
|
|
90
|
+
> │ │ ├─ index.vue —— 默认首页 /home
|
|
91
|
+
>
|
|
92
|
+
> │ │ ├─ meta.json —— 页面 meta 信息
|
|
93
|
+
>
|
|
94
|
+
> │ │ └─ [id]/ —— 动态参数路由示例
|
|
95
|
+
>
|
|
96
|
+
> │ │ └─ [name].vue —— 路由 /home/:id/:name
|
|
97
|
+
>
|
|
98
|
+
> │ ├─ App.vue —— 根组件
|
|
99
|
+
>
|
|
100
|
+
> │ ├─ main.js / main.ts —— 项目启动入口
|
|
101
|
+
>
|
|
102
|
+
> │ └─ style.css —— 全局样式文件
|
|
103
|
+
>
|
|
104
|
+
> ├─ index.html —— 项目入口页面
|
|
105
|
+
>
|
|
106
|
+
> ├─ jsconfig.json / tsconfig.json —— 路径别名与编译配置
|
|
107
|
+
>
|
|
108
|
+
> ├─ package.json —— 项目依赖与脚本配置
|
|
109
|
+
>
|
|
110
|
+
> ├─ postcss.config.mjs —— Tailwind CSS 配置文件(可选)
|
|
111
|
+
>
|
|
112
|
+
> ├─ README.md —— 项目说明文档
|
|
113
|
+
>
|
|
114
|
+
> └─ vite.config.ts —— Vite 开发与构建配置
|
|
101
115
|
|
|
102
116
|
---
|
|
103
117
|
|
|
104
|
-
## ⚙️
|
|
118
|
+
## ⚙️ 使用方式(多包管理器支持)
|
|
105
119
|
|
|
106
120
|
1. 创建项目
|
|
121
|
+
|
|
107
122
|
```bash
|
|
108
123
|
npm create vite-vue@latest
|
|
124
|
+
pnpm create vite-vue@latest
|
|
109
125
|
```
|
|
110
126
|
|
|
111
127
|
2. 进入项目目录
|
|
128
|
+
|
|
112
129
|
```bash
|
|
113
130
|
cd 项目名
|
|
114
131
|
```
|
|
115
132
|
|
|
116
|
-
3. 安装依赖
|
|
133
|
+
3. 安装依赖
|
|
134
|
+
|
|
117
135
|
```bash
|
|
118
136
|
npm install
|
|
137
|
+
pnpm install
|
|
119
138
|
```
|
|
120
139
|
|
|
121
140
|
4. 启动开发环境
|
|
141
|
+
|
|
122
142
|
```bash
|
|
123
143
|
npm run dev
|
|
144
|
+
pnpm dev
|
|
124
145
|
```
|
|
125
146
|
|
|
126
|
-
> 如果在创建时选择了「立即运行」,工具会自动执行启动命令。
|
|
127
|
-
|
|
128
|
-
---
|
|
129
|
-
|
|
130
|
-
## 🌐 技术栈官网链接
|
|
131
|
-
|
|
132
|
-
- [Vite](https://vitejs.dev/)
|
|
133
|
-
- [Vue 3](https://vuejs.org/)
|
|
134
|
-
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
135
|
-
- [TypeScript](https://www.typescriptlang.org/)
|
|
136
|
-
- [Vue Router](https://router.vuejs.org/)
|
|
137
|
-
- [Pinia](https://pinia.vuejs.org/)
|
|
138
|
-
- [Axios](https://axios-http.com/)
|
|
139
|
-
- [Element Plus](https://element-plus.org/)
|
|
140
|
-
- [Vant](https://vant-contrib.gitee.io/vant/)
|
|
141
|
-
- [VueUse](https://vueuse.org/)
|
|
142
|
-
- [Lodash](https://lodash.com/)
|
|
143
|
-
- [Day.js](https://day.js.org/)
|
|
144
|
-
- [Tailwind CSS](https://tailwindcss.com/)
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
|
-
## 🟢 Node.js 版本管理(.nvmrc)
|
|
149
|
-
|
|
150
|
-
项目根目录中包含一个 **`.nvmrc`** 文件,用于指定当前项目推荐使用的**Node.js 版本**。这样可以保证不同开发者或不同机器运行项目时使用一致的 Node环境,避免依赖问题。
|
|
151
|
-
|
|
152
|
-
### 使用 nvm 切换 Node 版本
|
|
153
|
-
|
|
154
|
-
如果你安装了 **nvm(Node Version Manager)**,进入项目目录后执行:
|
|
155
|
-
|
|
156
|
-
``` bash
|
|
157
|
-
nvm use
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
nvm 会自动读取 `.nvmrc` 文件中的 Node 版本并切换。
|
|
161
|
-
|
|
162
|
-
### Windows 可能出现的问题
|
|
163
|
-
|
|
164
|
-
在 **Windows PowerShell** 中,`nvm use` 有时不会自动读取`.nvmrc`,可能会报错或无法切换版本。
|
|
165
|
-
|
|
166
|
-
如果出现这种情况,可以执行:
|
|
167
|
-
|
|
168
|
-
``` powershell
|
|
169
|
-
nvm use $(Get-Content .nvmrc)
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
该命令会读取 `.nvmrc` 文件内容并手动切换 Node 版本。
|
|
173
|
-
|
|
174
147
|
---
|
|
175
148
|
|
|
176
149
|
## 🔧 常见需要调整的地方(具体文件示例)
|
|
177
150
|
|
|
178
151
|
### 1️⃣ 接口请求地址
|
|
179
152
|
|
|
180
|
-
文件:`src/utils/request.ts`
|
|
153
|
+
文件:`src/utils/request.ts` / `src/utils/request.js`
|
|
154
|
+
|
|
181
155
|
```ts
|
|
182
156
|
import axios from 'axios';
|
|
183
157
|
|
|
@@ -187,7 +161,7 @@ const service = axios.create({
|
|
|
187
161
|
});
|
|
188
162
|
|
|
189
163
|
// 示例请求
|
|
190
|
-
export const getUserList = () =>
|
|
164
|
+
export const getUserList = () => service.get('/users');
|
|
191
165
|
|
|
192
166
|
export default service;
|
|
193
167
|
```
|
|
@@ -198,7 +172,8 @@ export default service;
|
|
|
198
172
|
|
|
199
173
|
### 2️⃣ 本地代理配置
|
|
200
174
|
|
|
201
|
-
文件:`vite.config.ts`
|
|
175
|
+
文件:`vite.config.ts` / `vite.config.js`
|
|
176
|
+
|
|
202
177
|
```ts
|
|
203
178
|
import { defineConfig } from 'vite';
|
|
204
179
|
import vue from '@vitejs/plugin-vue';
|
|
@@ -224,7 +199,8 @@ export default defineConfig({
|
|
|
224
199
|
|
|
225
200
|
### 3️⃣ 路由结构(支持动态参数)
|
|
226
201
|
|
|
227
|
-
文件:`src/router/index.ts`
|
|
202
|
+
文件:`src/router/index.ts` / `src/router/index.js`
|
|
203
|
+
|
|
228
204
|
```ts
|
|
229
205
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
230
206
|
import routes from '~pages' // 自动生成的路由
|
|
@@ -237,14 +213,16 @@ const router = createRouter({
|
|
|
237
213
|
export default router
|
|
238
214
|
```
|
|
239
215
|
|
|
240
|
-
>
|
|
216
|
+
> 页面文件夹结构示例:
|
|
217
|
+
|
|
241
218
|
```text
|
|
242
219
|
src/views/home/
|
|
243
220
|
├─ index.vue -> /home
|
|
244
221
|
└─ [id]/[name].vue -> /home/:id/:name
|
|
245
222
|
```
|
|
246
223
|
|
|
247
|
-
>
|
|
224
|
+
> 获取路由参数:
|
|
225
|
+
|
|
248
226
|
```ts
|
|
249
227
|
import { useRoute } from 'vue-router'
|
|
250
228
|
const route = useRoute()
|
|
@@ -257,6 +235,7 @@ console.log(route.params.name)
|
|
|
257
235
|
### 4️⃣ 页面内容与样式
|
|
258
236
|
|
|
259
237
|
文件示例:`src/views/home/index.vue`
|
|
238
|
+
|
|
260
239
|
```vue
|
|
261
240
|
<template>
|
|
262
241
|
<div class="home-container">
|
|
@@ -281,6 +260,64 @@ console.log(route.params.name)
|
|
|
281
260
|
|
|
282
261
|
---
|
|
283
262
|
|
|
263
|
+
### 5️⃣ Mitt 事件总线使用示例
|
|
264
|
+
|
|
265
|
+
#### 封装事件总线
|
|
266
|
+
|
|
267
|
+
文件:`src/utils/eventBus.ts`
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
import mitt from 'mitt'
|
|
271
|
+
|
|
272
|
+
type Events = {
|
|
273
|
+
userLogin: { name: string },
|
|
274
|
+
userLogout: null
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const emitter = mitt<Events>()
|
|
278
|
+
|
|
279
|
+
export default emitter
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### 使用示例
|
|
283
|
+
|
|
284
|
+
##### 组件 A(发送事件)
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
<script setup lang="ts">
|
|
288
|
+
import eventBus from '@/utils/eventBus'
|
|
289
|
+
|
|
290
|
+
function login() {
|
|
291
|
+
eventBus.emit('userLogin', { name: '张三' })
|
|
292
|
+
}
|
|
293
|
+
</script>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
##### 组件 B(接收事件)
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
<script setup lang="ts">
|
|
300
|
+
import { onMounted, onUnmounted } from 'vue'
|
|
301
|
+
import eventBus from '@/utils/eventBus'
|
|
302
|
+
|
|
303
|
+
function handleLogin(payload: { name: string }) {
|
|
304
|
+
console.log('用户登录:', payload.name)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
onMounted(() => {
|
|
308
|
+
eventBus.on('userLogin', handleLogin)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
onUnmounted(() => {
|
|
312
|
+
eventBus.off('userLogin', handleLogin)
|
|
313
|
+
})
|
|
314
|
+
</script>
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
> 🔹 Mitt 事件总线轻量高效,适合组件间解耦通信,替代父子传递 props / emit 或 Vuex/Pinia 中的小型事件场景。
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
284
321
|
## 🎯 适用场景
|
|
285
322
|
|
|
286
323
|
- Vue 3 新手快速上手
|
|
@@ -291,6 +328,25 @@ console.log(route.params.name)
|
|
|
291
328
|
|
|
292
329
|
---
|
|
293
330
|
|
|
331
|
+
## 🌐 技术栈官网链接
|
|
332
|
+
|
|
333
|
+
- [Vite | 下一代的前端工具链](https://cn.vitejs.dev/)
|
|
334
|
+
- [Vue.js - 渐进式 JavaScript 框架 | Vue.js](https://cn.vuejs.org/)
|
|
335
|
+
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
336
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
337
|
+
- [Vue Router | Vue.js 的官方路由](https://router.vuejs.org/zh/)
|
|
338
|
+
- [Pinia | The intuitive store for Vue.js](https://pinia.vuejs.org/zh/)
|
|
339
|
+
- [Axios](https://www.axios-http.cn/)
|
|
340
|
+
- [Element Plus | 一个 Vue 3 UI 框架](https://element-plus.org/zh-CN/)
|
|
341
|
+
- [Vant 4 - 轻量、可定制的移动端组件库](https://vant-ui.github.io/vant/#/zh-CN)
|
|
342
|
+
- [VueUse](https://vueuse.pages.dev/)
|
|
343
|
+
- [Lodash](https://www.lodashjs.com/)
|
|
344
|
+
- [Day.js](https://day.js.org/)
|
|
345
|
+
- [Tailwind CSS](https://tailwindcss.com/)
|
|
346
|
+
- [Mitt](https://github.com/developit/mitt)
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
294
350
|
## 📄 License
|
|
295
351
|
|
|
296
352
|
MIT License
|
|
@@ -11,13 +11,10 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"less": "^4.5.1",
|
|
13
13
|
"tslib": "^2.8.1",
|
|
14
|
-
"vue": "^3.5.
|
|
14
|
+
"vue": "^3.5.30"__OPTIONAL_DEP__
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@vitejs/plugin-vue": "^6.0.
|
|
18
|
-
"vite": "^8.0.
|
|
19
|
-
},
|
|
20
|
-
"overrides": {
|
|
21
|
-
"vite": "^8.0.0-beta.13"
|
|
17
|
+
"@vitejs/plugin-vue": "^6.0.5",
|
|
18
|
+
"vite": "^8.0.1"
|
|
22
19
|
}
|
|
23
20
|
}
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
- 🌐 本地及网络访问启动日志显示
|
|
20
20
|
- 📝 自定义 Banner 插件显示项目信息
|
|
21
21
|
- 🎨 可选集成 Tailwind CSS(通过 postcss 配置)
|
|
22
|
+
- 🟢 支持多种包管理器:npm / pnpm,可根据环境自动识别并使用
|
|
23
|
+
- 🔔 可选集成 Mitt(轻量事件总线,实现组件间解耦通信)
|
|
22
24
|
|
|
23
25
|
---
|
|
24
26
|
|
|
@@ -33,6 +35,7 @@
|
|
|
33
35
|
🖥️ Element Plus / 📱 Vant
|
|
34
36
|
🧰 VueUse · Lodash · Day.js
|
|
35
37
|
🎨 Tailwind CSS
|
|
38
|
+
🔔 Mitt
|
|
36
39
|
|
|
37
40
|
---
|
|
38
41
|
|
|
@@ -58,126 +61,97 @@
|
|
|
58
61
|
|
|
59
62
|
## 📁 项目目录结构说明
|
|
60
63
|
|
|
61
|
-
project-name
|
|
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
|
-
|
|
64
|
+
> project-name
|
|
65
|
+
>
|
|
66
|
+
> ├─ public/ —— 公共静态资源目录
|
|
67
|
+
>
|
|
68
|
+
> │ └─ favicon.ico
|
|
69
|
+
>
|
|
70
|
+
> ├─ src/
|
|
71
|
+
>
|
|
72
|
+
> │ ├─ api/ —— 接口请求封装目录
|
|
73
|
+
>
|
|
74
|
+
> │ ├─ assets/ —— 图片、字体等静态资源
|
|
75
|
+
>
|
|
76
|
+
> │ ├─ components/ —— 公共组件目录
|
|
77
|
+
>
|
|
78
|
+
> │ ├─ router/ —— 路由配置(可选)
|
|
79
|
+
>
|
|
80
|
+
> │ ├─ stores/ —— Pinia 状态管理(可选)
|
|
81
|
+
>
|
|
82
|
+
> │ ├─ types/ —— 类型声明文件
|
|
83
|
+
>
|
|
84
|
+
> │ ├─ utils/ —— 工具方法、请求封装
|
|
85
|
+
>
|
|
86
|
+
> │ ├─ views/ —— 页面级组件(路由页面)
|
|
87
|
+
>
|
|
88
|
+
> │ │ └─ home/ —— 示例页面文件夹
|
|
89
|
+
>
|
|
90
|
+
> │ │ ├─ index.vue —— 默认首页 /home
|
|
91
|
+
>
|
|
92
|
+
> │ │ ├─ meta.json —— 页面 meta 信息
|
|
93
|
+
>
|
|
94
|
+
> │ │ └─ [id]/ —— 动态参数路由示例
|
|
95
|
+
>
|
|
96
|
+
> │ │ └─ [name].vue —— 路由 /home/:id/:name
|
|
97
|
+
>
|
|
98
|
+
> │ ├─ App.vue —— 根组件
|
|
99
|
+
>
|
|
100
|
+
> │ ├─ main.js / main.ts —— 项目启动入口
|
|
101
|
+
>
|
|
102
|
+
> │ └─ style.css —— 全局样式文件
|
|
103
|
+
>
|
|
104
|
+
> ├─ index.html —— 项目入口页面
|
|
105
|
+
>
|
|
106
|
+
> ├─ jsconfig.json / tsconfig.json —— 路径别名与编译配置
|
|
107
|
+
>
|
|
108
|
+
> ├─ package.json —— 项目依赖与脚本配置
|
|
109
|
+
>
|
|
110
|
+
> ├─ postcss.config.mjs —— Tailwind CSS 配置文件(可选)
|
|
111
|
+
>
|
|
112
|
+
> ├─ README.md —— 项目说明文档
|
|
113
|
+
>
|
|
114
|
+
> └─ vite.config.ts —— Vite 开发与构建配置
|
|
101
115
|
|
|
102
116
|
---
|
|
103
117
|
|
|
104
|
-
## ⚙️
|
|
118
|
+
## ⚙️ 使用方式(多包管理器支持)
|
|
105
119
|
|
|
106
120
|
1. 创建项目
|
|
121
|
+
|
|
107
122
|
```bash
|
|
108
123
|
npm create vite-vue@latest
|
|
124
|
+
pnpm create vite-vue@latest
|
|
109
125
|
```
|
|
110
126
|
|
|
111
127
|
2. 进入项目目录
|
|
128
|
+
|
|
112
129
|
```bash
|
|
113
130
|
cd 项目名
|
|
114
131
|
```
|
|
115
132
|
|
|
116
|
-
3. 安装依赖
|
|
133
|
+
3. 安装依赖
|
|
134
|
+
|
|
117
135
|
```bash
|
|
118
136
|
npm install
|
|
137
|
+
pnpm install
|
|
119
138
|
```
|
|
120
139
|
|
|
121
140
|
4. 启动开发环境
|
|
141
|
+
|
|
122
142
|
```bash
|
|
123
143
|
npm run dev
|
|
144
|
+
pnpm dev
|
|
124
145
|
```
|
|
125
146
|
|
|
126
|
-
> 如果在创建时选择了「立即运行」,工具会自动执行启动命令。
|
|
127
|
-
|
|
128
|
-
---
|
|
129
|
-
|
|
130
|
-
## 🌐 技术栈官网链接
|
|
131
|
-
|
|
132
|
-
- [Vite](https://vitejs.dev/)
|
|
133
|
-
- [Vue 3](https://vuejs.org/)
|
|
134
|
-
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
135
|
-
- [TypeScript](https://www.typescriptlang.org/)
|
|
136
|
-
- [Vue Router](https://router.vuejs.org/)
|
|
137
|
-
- [Pinia](https://pinia.vuejs.org/)
|
|
138
|
-
- [Axios](https://axios-http.com/)
|
|
139
|
-
- [Element Plus](https://element-plus.org/)
|
|
140
|
-
- [Vant](https://vant-contrib.gitee.io/vant/)
|
|
141
|
-
- [VueUse](https://vueuse.org/)
|
|
142
|
-
- [Lodash](https://lodash.com/)
|
|
143
|
-
- [Day.js](https://day.js.org/)
|
|
144
|
-
- [Tailwind CSS](https://tailwindcss.com/)
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
|
-
## 🟢 Node.js 版本管理(.nvmrc)
|
|
149
|
-
|
|
150
|
-
项目根目录中包含一个 **`.nvmrc`** 文件,用于指定当前项目推荐使用的**Node.js 版本**。这样可以保证不同开发者或不同机器运行项目时使用一致的 Node环境,避免依赖问题。
|
|
151
|
-
|
|
152
|
-
### 使用 nvm 切换 Node 版本
|
|
153
|
-
|
|
154
|
-
如果你安装了 **nvm(Node Version Manager)**,进入项目目录后执行:
|
|
155
|
-
|
|
156
|
-
``` bash
|
|
157
|
-
nvm use
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
nvm 会自动读取 `.nvmrc` 文件中的 Node 版本并切换。
|
|
161
|
-
|
|
162
|
-
### Windows 可能出现的问题
|
|
163
|
-
|
|
164
|
-
在 **Windows PowerShell** 中,`nvm use` 有时不会自动读取`.nvmrc`,可能会报错或无法切换版本。
|
|
165
|
-
|
|
166
|
-
如果出现这种情况,可以执行:
|
|
167
|
-
|
|
168
|
-
``` powershell
|
|
169
|
-
nvm use $(Get-Content .nvmrc)
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
该命令会读取 `.nvmrc` 文件内容并手动切换 Node 版本。
|
|
173
|
-
|
|
174
147
|
---
|
|
175
148
|
|
|
176
149
|
## 🔧 常见需要调整的地方(具体文件示例)
|
|
177
150
|
|
|
178
151
|
### 1️⃣ 接口请求地址
|
|
179
152
|
|
|
180
|
-
文件:`src/utils/request.ts`
|
|
153
|
+
文件:`src/utils/request.ts` / `src/utils/request.js`
|
|
154
|
+
|
|
181
155
|
```ts
|
|
182
156
|
import axios from 'axios';
|
|
183
157
|
|
|
@@ -187,7 +161,7 @@ const service = axios.create({
|
|
|
187
161
|
});
|
|
188
162
|
|
|
189
163
|
// 示例请求
|
|
190
|
-
export const getUserList = () =>
|
|
164
|
+
export const getUserList = () => service.get('/users');
|
|
191
165
|
|
|
192
166
|
export default service;
|
|
193
167
|
```
|
|
@@ -198,7 +172,8 @@ export default service;
|
|
|
198
172
|
|
|
199
173
|
### 2️⃣ 本地代理配置
|
|
200
174
|
|
|
201
|
-
文件:`vite.config.ts`
|
|
175
|
+
文件:`vite.config.ts` / `vite.config.js`
|
|
176
|
+
|
|
202
177
|
```ts
|
|
203
178
|
import { defineConfig } from 'vite';
|
|
204
179
|
import vue from '@vitejs/plugin-vue';
|
|
@@ -224,7 +199,8 @@ export default defineConfig({
|
|
|
224
199
|
|
|
225
200
|
### 3️⃣ 路由结构(支持动态参数)
|
|
226
201
|
|
|
227
|
-
文件:`src/router/index.ts`
|
|
202
|
+
文件:`src/router/index.ts` / `src/router/index.js`
|
|
203
|
+
|
|
228
204
|
```ts
|
|
229
205
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
230
206
|
import routes from '~pages' // 自动生成的路由
|
|
@@ -237,14 +213,16 @@ const router = createRouter({
|
|
|
237
213
|
export default router
|
|
238
214
|
```
|
|
239
215
|
|
|
240
|
-
>
|
|
216
|
+
> 页面文件夹结构示例:
|
|
217
|
+
|
|
241
218
|
```text
|
|
242
219
|
src/views/home/
|
|
243
220
|
├─ index.vue -> /home
|
|
244
221
|
└─ [id]/[name].vue -> /home/:id/:name
|
|
245
222
|
```
|
|
246
223
|
|
|
247
|
-
>
|
|
224
|
+
> 获取路由参数:
|
|
225
|
+
|
|
248
226
|
```ts
|
|
249
227
|
import { useRoute } from 'vue-router'
|
|
250
228
|
const route = useRoute()
|
|
@@ -257,6 +235,7 @@ console.log(route.params.name)
|
|
|
257
235
|
### 4️⃣ 页面内容与样式
|
|
258
236
|
|
|
259
237
|
文件示例:`src/views/home/index.vue`
|
|
238
|
+
|
|
260
239
|
```vue
|
|
261
240
|
<template>
|
|
262
241
|
<div class="home-container">
|
|
@@ -281,6 +260,64 @@ console.log(route.params.name)
|
|
|
281
260
|
|
|
282
261
|
---
|
|
283
262
|
|
|
263
|
+
### 5️⃣ Mitt 事件总线使用示例
|
|
264
|
+
|
|
265
|
+
#### 封装事件总线
|
|
266
|
+
|
|
267
|
+
文件:`src/utils/eventBus.ts`
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
import mitt from 'mitt'
|
|
271
|
+
|
|
272
|
+
type Events = {
|
|
273
|
+
userLogin: { name: string },
|
|
274
|
+
userLogout: null
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const emitter = mitt<Events>()
|
|
278
|
+
|
|
279
|
+
export default emitter
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### 使用示例
|
|
283
|
+
|
|
284
|
+
##### 组件 A(发送事件)
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
<script setup lang="ts">
|
|
288
|
+
import eventBus from '@/utils/eventBus'
|
|
289
|
+
|
|
290
|
+
function login() {
|
|
291
|
+
eventBus.emit('userLogin', { name: '张三' })
|
|
292
|
+
}
|
|
293
|
+
</script>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
##### 组件 B(接收事件)
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
<script setup lang="ts">
|
|
300
|
+
import { onMounted, onUnmounted } from 'vue'
|
|
301
|
+
import eventBus from '@/utils/eventBus'
|
|
302
|
+
|
|
303
|
+
function handleLogin(payload: { name: string }) {
|
|
304
|
+
console.log('用户登录:', payload.name)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
onMounted(() => {
|
|
308
|
+
eventBus.on('userLogin', handleLogin)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
onUnmounted(() => {
|
|
312
|
+
eventBus.off('userLogin', handleLogin)
|
|
313
|
+
})
|
|
314
|
+
</script>
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
> 🔹 Mitt 事件总线轻量高效,适合组件间解耦通信,替代父子传递 props / emit 或 Vuex/Pinia 中的小型事件场景。
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
284
321
|
## 🎯 适用场景
|
|
285
322
|
|
|
286
323
|
- Vue 3 新手快速上手
|
|
@@ -291,6 +328,25 @@ console.log(route.params.name)
|
|
|
291
328
|
|
|
292
329
|
---
|
|
293
330
|
|
|
331
|
+
## 🌐 技术栈官网链接
|
|
332
|
+
|
|
333
|
+
- [Vite | 下一代的前端工具链](https://cn.vitejs.dev/)
|
|
334
|
+
- [Vue.js - 渐进式 JavaScript 框架 | Vue.js](https://cn.vuejs.org/)
|
|
335
|
+
- [JavaScript](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript)
|
|
336
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
337
|
+
- [Vue Router | Vue.js 的官方路由](https://router.vuejs.org/zh/)
|
|
338
|
+
- [Pinia | The intuitive store for Vue.js](https://pinia.vuejs.org/zh/)
|
|
339
|
+
- [Axios](https://www.axios-http.cn/)
|
|
340
|
+
- [Element Plus | 一个 Vue 3 UI 框架](https://element-plus.org/zh-CN/)
|
|
341
|
+
- [Vant 4 - 轻量、可定制的移动端组件库](https://vant-ui.github.io/vant/#/zh-CN)
|
|
342
|
+
- [VueUse](https://vueuse.pages.dev/)
|
|
343
|
+
- [Lodash](https://www.lodashjs.com/)
|
|
344
|
+
- [Day.js](https://day.js.org/)
|
|
345
|
+
- [Tailwind CSS](https://tailwindcss.com/)
|
|
346
|
+
- [Mitt](https://github.com/developit/mitt)
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
294
350
|
## 📄 License
|
|
295
351
|
|
|
296
352
|
MIT License
|
|
@@ -11,17 +11,14 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"less": "^4.5.1",
|
|
13
13
|
"tslib": "^2.8.1",
|
|
14
|
-
"vue": "^3.5.
|
|
14
|
+
"vue": "^3.5.30"__OPTIONAL_DEP__
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@types/node": "^24.
|
|
18
|
-
"@vitejs/plugin-vue": "^6.0.
|
|
19
|
-
"@vue/tsconfig": "^0.
|
|
17
|
+
"@types/node": "^24.12.0",
|
|
18
|
+
"@vitejs/plugin-vue": "^6.0.5",
|
|
19
|
+
"@vue/tsconfig": "^0.9.0",
|
|
20
20
|
"typescript": "~5.9.3",
|
|
21
|
-
"vite": "^8.0.
|
|
22
|
-
"vue-tsc": "^3.
|
|
23
|
-
},
|
|
24
|
-
"overrides": {
|
|
25
|
-
"vite": "^8.0.0-beta.13"
|
|
21
|
+
"vite": "^8.0.1",
|
|
22
|
+
"vue-tsc": "^3.2.5"
|
|
26
23
|
}
|
|
27
24
|
}
|