From 225fdd0175e089392586602f4f9828e00d471d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=89=E6=9D=A5=E6=8A=80=E6=9C=AF?= <1490493387@qq.com> Date: Tue, 16 Nov 2021 01:05:20 +0000 Subject: [PATCH] =?UTF-8?q?feat:=E4=BF=AE=E6=94=B9=E5=A4=9A=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE=E5=92=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=99=BA=E8=83=BD=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 8 +++++--- .env.production | 8 +++----- .env.staging | 8 +++----- README.md | 40 +++++++++++++++++++++++++--------------- src/env.d.ts | 8 ++++++++ 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/.env.development b/.env.development index 891be06b..ee6c1c96 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,7 @@ # 开发环境变量 -ENV = 'development' -# base api -VITE_BASE_API = '/dev-api' +# 变量必须以 VITE_ 为前缀才能暴露给外部读取 + +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/dev-api' diff --git a/.env.production b/.env.production index 3ccdefb0..89e04cae 100644 --- a/.env.production +++ b/.env.production @@ -1,6 +1,4 @@ # 生产环境变量 -ENV = 'production' - -# base api -VITE_BASE_API = '/prod-api' - +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/prod-api' diff --git a/.env.staging b/.env.staging index 21ea2060..da543f6a 100644 --- a/.env.staging +++ b/.env.staging @@ -1,6 +1,4 @@ # 模拟环境变量 -ENV = 'staging' - -# base api -VITE_BASE_API = '/stage-api' - +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/stage-api' diff --git a/README.md b/README.md index 16deb6cc..07fe6f7b 100644 --- a/README.md +++ b/README.md @@ -265,45 +265,55 @@ export default defineComponent({ ## Vite 环境变量配置 -**官方说明:** https://cn.vitejs.dev/guide/env-and-mode.html +**官方环境变量配置文档:** https://cn.vitejs.dev/guide/env-and-mode.html -项目根目录添加多个环境的配置文件 +#### 多环境配置 开发环境变量文件:`.env.development` ```properties # 开发环境变量 -ENV = 'development' -# base api -VITE_BASE_API = '/dev-api' +# 变量必须以 VITE_ 为前缀才能暴露给外部读取 + +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/dev-api' + ``` - 生产环境变量文件:`.env.production` ```properties # 生产环境变量 -ENV = 'production' - -# base api -VITE_BASE_API = '/prod-api' +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/prod-api' ``` - 模拟环境变量文件:`.env.staging` ```properties # 模拟环境变量 -ENV = 'staging' - -# base api -VITE_BASE_API = '/stage-api' +VITE_APP_TITLE = '管理系统' +VITE_APP_PORT = 3000 +VITE_APP_BASE_API = '/stage-api' ``` +#### 环境变量智能提示 +`src/env.d.ts` 添加以下配置 + +```typescript +// 环境变量智能提示 +interface ImportMetaEnv { + VITE_APP_TITLE: string, + VITE_APP_PORT: string, + VITE_APP_BASE_API: string +} +``` ## 生产打包配置 diff --git a/src/env.d.ts b/src/env.d.ts index d27eb5a3..e8b0bee7 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -6,3 +6,11 @@ declare module '*.vue' { const component: DefineComponent<{}, {}, any> export default component } + + +// 环境变量智能提示 +interface ImportMetaEnv { + VITE_APP_TITLE: string, + VITE_APP_PORT: string, + VITE_APP_BASE_API: string +} \ No newline at end of file