配置初始化

配置路径别名

步骤1:安装 @types/node

为了确保 TypeScript 能够正确地为 Node.js 代码提供类型检查和自动补全功能,我们需要先安装 @types/node

1pnpm add -D @types/node

步骤2:Vite 配置 (vite.config.ts):

Vite 在构建(或开发服务器启动)时使用这些别名。这意味着它们影响的是最终打包的代码,以及在开发服务器中代码的实时重载。

1import { fileURLToPath, URL } from "node:url" // [!code focus]// [!code ++]
2import { defineConfig } from "vite"
3import vue from "@vitejs/plugin-vue"
4
5// https://vite.dev/config/
6export default defineConfig({
7	resolve: { // [!code focus]// [!code ++]
8		alias: { // [!code focus]// [!code ++]
9			"@": fileURLToPath(new URL("./src", import.meta.url)), // [!code focus]// [!code ++]
10		}, // [!code focus]// [!code ++]
11	}, // [!code focus]// [!code ++]
12	plugins: [vue()],
13})

步骤3:TypeScript 配置 (tsconfig.app.json):

TypeScript 在编译时使用这些别名。这意味着它们主要用于类型检查和编译 TypeScript 代码到 JavaScript,而不直接影响最终打包的代码。

1{
2	"compilerOptions": {
3		"baseUrl": "./", // [!code focus]// [!code ++]
4		"paths": { // [!code focus]// [!code ++]
5			"@/*": ["src/*"] // [!code focus]// [!code ++]
6		} // [!code focus]// [!code ++]
7	}
8}

这样,你可以在项目中使用 @ 别名来引用 src 目录下的文件,提高代码的可读性和维护性。

配置环境变量

步骤 1:创建环境变量文件

在项目根目录下创建 .env 文件以及针对不同环境的文件:.env.development.env.staging.env.production

文件 说明
.env 所有情况下都会加载
.env.local 所有情况下都会加载,但会被 git 忽略
.env.[mode] 只在指定模式下加载
.env.[mode].local 只在指定模式下加载,但会被 git 忽略
1# 所有环境共享的环境变量
2
3## 项目标题
4VITE_APP_TITLE = Vue3 Admin Template
1# 开发环境专用的环境变量
2
3## 后端接口地址(如果解决跨域问题采用反向代理就只需写相对路径)
4VITE_BASE_URL = /api
1# 预发布环境专用的环境变量
2
3## 后端接口地址(如果解决跨域问题采用 CORS 就需要写绝对路径)
4VITE_BASE_URL = https://st.codebetter.cn/api
1# 生产环境专用的环境变量
2
3## 后端接口地址(如果解决跨域问题采用 CORS 就需要写绝对路径)
4VITE_BASE_URL = https://prod.codebetter.cn/api

:::


步骤 2:在代码中访问环境变量

只有以 VITE_ 为前缀的变量才会暴露给经过 vite 处理的代码。在代码中,可以通过 import.meta.env 访问这些环境变量。

1console.log(import.meta.env.VITE_APP_TITLE) // Vue3 Admin Template

步骤 3:配置构建脚本

在 package.json 文件中配置不同的构建脚本,以便于针对不同环境进行构建。

1{
2  "scripts": {
3    "dev": "vite --open",
4    "build:test": "vue-tsc -b && vite build --mode development",
5    "build:st": "vue-tsc -b && vite build --mode staging",
6    "build:prod": "vue-tsc -b && vite build --mode production",
7  }
8}

通过这些步骤,您可以根据不同的环境配置不同的环境变量,并且能够通过构建脚本来控制项目的构建过程。

CSS重置

reset.cssnormalize.css 是两种流行的样式重置方法,任选其一即可。

reset.css
1/* http://meyerweb.com/eric/tools/css/reset/
2   v5.0.1 | 20191019
3   License: none (public domain)
4*/
5
6html,
7body,
8div,
9span,
10applet,
11object,
12iframe,
13h1,
14h2,
15h3,
16h4,
17h5,
18h6,
19p,
20blockquote,
21pre,
22a,
23abbr,
24acronym,
25address,
26big,
27cite,
28code,
29del,
30dfn,
31em,
32img,
33ins,
34kbd,
35q,
36s,
37samp,
38small,
39strike,
40strong,
41sub,
42sup,
43tt,
44var,
45b,
46u,
47i,
48center,
49dl,
50dt,
51dd,
52ol,
53ul,
54li,
55fieldset,
56form,
57label,
58legend,
59table,
60caption,
61tbody,
62tfoot,
63thead,
64tr,
65th,
66td,
67article,
68aside,
69canvas,
70details,
71embed,
72figure,
73figcaption,
74footer,
75header,
76hgroup,
77main,
78menu,
79nav,
80output,
81ruby,
82section,
83summary,
84time,
85mark,
86audio,
87video {
88	margin: 0;
89	padding: 0;
90	border: 0;
91	font-size: 100%;
92	font: inherit;
93	vertical-align: baseline;
94}
95/* HTML5 display-role reset for older browsers */
96article,
97aside,
98details,
99figcaption,
100figure,
101footer,
102header,
103hgroup,
104main,
105menu,
106nav,
107section {
108	display: block;
109}
110/* HTML5 hidden-attribute fix for newer browsers */
111*[hidden] {
112	display: none;
113}
114body {
115	line-height: 1;
116}
117menu,
118ol,
119ul {
120	list-style: none;
121}
122blockquote,
123q {
124	quotes: none;
125}
126blockquote:before,
127blockquote:after,
128q:before,
129q:after {
130	content: "";
131	content: none;
132}
133table {
134	border-collapse: collapse;
135	border-spacing: 0;
136}
normalize.css
1/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
2
3/* Document
4   ========================================================================== */
5
6/**
7 * 1. Correct the line height in all browsers.
8 * 2. Prevent adjustments of font size after orientation changes in iOS.
9 */
10
11html {
12	line-height: 1.15; /* 1 */
13	-webkit-text-size-adjust: 100%; /* 2 */
14}
15
16/* Sections
17   ========================================================================== */
18
19/**
20 * Remove the margin in all browsers.
21 */
22
23body {
24	margin: 0;
25}
26
27/**
28 * Render the `main` element consistently in IE.
29 */
30
31main {
32	display: block;
33}
34
35/**
36 * Correct the font size and margin on `h1` elements within `section` and
37 * `article` contexts in Chrome, Firefox, and Safari.
38 */
39
40h1 {
41	font-size: 2em;
42	margin: 0.67em 0;
43}
44
45/* Grouping content
46   ========================================================================== */
47
48/**
49 * 1. Add the correct box sizing in Firefox.
50 * 2. Show the overflow in Edge and IE.
51 */
52
53hr {
54	box-sizing: content-box; /* 1 */
55	height: 0; /* 1 */
56	overflow: visible; /* 2 */
57}
58
59/**
60 * 1. Correct the inheritance and scaling of font size in all browsers.
61 * 2. Correct the odd `em` font sizing in all browsers.
62 */
63
64pre {
65	font-family: monospace, monospace; /* 1 */
66	font-size: 1em; /* 2 */
67}
68
69/* Text-level semantics
70   ========================================================================== */
71
72/**
73 * Remove the gray background on active links in IE 10.
74 */
75
76a {
77	background-color: transparent;
78}
79
80/**
81 * 1. Remove the bottom border in Chrome 57-
82 * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
83 */
84
85abbr[title] {
86	border-bottom: none; /* 1 */
87	text-decoration: underline; /* 2 */
88	text-decoration: underline dotted; /* 2 */
89}
90
91/**
92 * Add the correct font weight in Chrome, Edge, and Safari.
93 */
94
95b,
96strong {
97	font-weight: bolder;
98}
99
100/**
101 * 1. Correct the inheritance and scaling of font size in all browsers.
102 * 2. Correct the odd `em` font sizing in all browsers.
103 */
104
105code,
106kbd,
107samp {
108	font-family: monospace, monospace; /* 1 */
109	font-size: 1em; /* 2 */
110}
111
112/**
113 * Add the correct font size in all browsers.
114 */
115
116small {
117	font-size: 80%;
118}
119
120/**
121 * Prevent `sub` and `sup` elements from affecting the line height in
122 * all browsers.
123 */
124
125sub,
126sup {
127	font-size: 75%;
128	line-height: 0;
129	position: relative;
130	vertical-align: baseline;
131}
132
133sub {
134	bottom: -0.25em;
135}
136
137sup {
138	top: -0.5em;
139}
140
141/* Embedded content
142   ========================================================================== */
143
144/**
145 * Remove the border on images inside links in IE 10.
146 */
147
148img {
149	border-style: none;
150}
151
152/* Forms
153   ========================================================================== */
154
155/**
156 * 1. Change the font styles in all browsers.
157 * 2. Remove the margin in Firefox and Safari.
158 */
159
160button,
161input,
162optgroup,
163select,
164textarea {
165	font-family: inherit; /* 1 */
166	font-size: 100%; /* 1 */
167	line-height: 1.15; /* 1 */
168	margin: 0; /* 2 */
169}
170
171/**
172 * Show the overflow in IE.
173 * 1. Show the overflow in Edge.
174 */
175
176button,
177input {
178	/* 1 */
179	overflow: visible;
180}
181
182/**
183 * Remove the inheritance of text transform in Edge, Firefox, and IE.
184 * 1. Remove the inheritance of text transform in Firefox.
185 */
186
187button,
188select {
189	/* 1 */
190	text-transform: none;
191}
192
193/**
194 * Correct the inability to style clickable types in iOS and Safari.
195 */
196
197button,
198[type="button"],
199[type="reset"],
200[type="submit"] {
201	-webkit-appearance: button;
202}
203
204/**
205 * Remove the inner border and padding in Firefox.
206 */
207
208button::-moz-focus-inner,
209[type="button"]::-moz-focus-inner,
210[type="reset"]::-moz-focus-inner,
211[type="submit"]::-moz-focus-inner {
212	border-style: none;
213	padding: 0;
214}
215
216/**
217 * Restore the focus styles unset by the previous rule.
218 */
219
220button:-moz-focusring,
221[type="button"]:-moz-focusring,
222[type="reset"]:-moz-focusring,
223[type="submit"]:-moz-focusring {
224	outline: 1px dotted ButtonText;
225}
226
227/**
228 * Correct the padding in Firefox.
229 */
230
231fieldset {
232	padding: 0.35em 0.75em 0.625em;
233}
234
235/**
236 * 1. Correct the text wrapping in Edge and IE.
237 * 2. Correct the color inheritance from `fieldset` elements in IE.
238 * 3. Remove the padding so developers are not caught out when they zero out
239 *    `fieldset` elements in all browsers.
240 */
241
242legend {
243	box-sizing: border-box; /* 1 */
244	color: inherit; /* 2 */
245	display: table; /* 1 */
246	max-width: 100%; /* 1 */
247	padding: 0; /* 3 */
248	white-space: normal; /* 1 */
249}
250
251/**
252 * Add the correct vertical alignment in Chrome, Firefox, and Opera.
253 */
254
255progress {
256	vertical-align: baseline;
257}
258
259/**
260 * Remove the default vertical scrollbar in IE 10+.
261 */
262
263textarea {
264	overflow: auto;
265}
266
267/**
268 * 1. Add the correct box sizing in IE 10.
269 * 2. Remove the padding in IE 10.
270 */
271
272[type="checkbox"],
273[type="radio"] {
274	box-sizing: border-box; /* 1 */
275	padding: 0; /* 2 */
276}
277
278/**
279 * Correct the cursor style of increment and decrement buttons in Chrome.
280 */
281
282[type="number"]::-webkit-inner-spin-button,
283[type="number"]::-webkit-outer-spin-button {
284	height: auto;
285}
286
287/**
288 * 1. Correct the odd appearance in Chrome and Safari.
289 * 2. Correct the outline style in Safari.
290 */
291
292[type="search"] {
293	-webkit-appearance: textfield; /* 1 */
294	outline-offset: -2px; /* 2 */
295}
296
297/**
298 * Remove the inner padding in Chrome and Safari on macOS.
299 */
300
301[type="search"]::-webkit-search-decoration {
302	-webkit-appearance: none;
303}
304
305/**
306 * 1. Correct the inability to style clickable types in iOS and Safari.
307 * 2. Change font properties to `inherit` in Safari.
308 */
309
310::-webkit-file-upload-button {
311	-webkit-appearance: button; /* 1 */
312	font: inherit; /* 2 */
313}
314
315/* Interactive
316   ========================================================================== */
317
318/*
319 * Add the correct display in Edge, IE 10+, and Firefox.
320 */
321
322details {
323	display: block;
324}
325
326/*
327 * Add the correct display in all browsers.
328 */
329
330summary {
331	display: list-item;
332}
333
334/* Misc
335   ========================================================================== */
336
337/**
338 * Add the correct display in IE 10+.
339 */
340
341template {
342	display: none;
343}
344
345/**
346 * Add the correct display in IE 10.
347 */
348
349[hidden] {
350	display: none;
351}