view2
vue2使用 process.env获取环境变量
view 3
process.env 在vue3中不生效
vue3中使用 import.meta.env 获取环境变量
import.meta.env
Vite exposes environment variables on a special object
Configuration
-
Create a new
.env.development
and.env.production
The file name must be defined like this, otherwise it cannot be read.
Write node environment variables and other variables . Other variables must
VITE_
start with-
node environment variables
- Must
NODE_
start with NODE_
The value after will overwrite the previous value.
- Must
-
Other variables
- Must
VITE_
start with - If it doesn’t
VITE_
start with, it won’t be VITE_
The key-value pairs at the beginning will not overwrite the previous ones.
- Must
Create a file
.env.development
-
# 开发环境配置
NODE_ENV='development'
# 本地服务端口
VITE_PORT=3000
# 页面标题
VITE_TITLE='**********系统-开发'
# 超时时间(ms)
VITE_AXIOS_TIMEOUT=60000
# 删除 console
VITE_DROP_CONSOLE=false;
创建文件`.env.production`
# 开发环境配置
NODE_ENV='production'
# 本地服务端口
VITE_PORT=3001
# 页面标题
VITE_TITLE='**********系统'
# 超时时间(ms)
VITE_AXIOS_TIMEOUT=6000
# 删除 console
VITE_DROP_CONSOLE=true;
tsconfig.json
Added in to"types": [ "vite/client" ]
provideimport.meta.env
type definitions for environment variables injected on Vite
"compilerOptions": {
"types": [ "vite/client" ]
}
src目录
Create a file underenv.d.ts
and add the following code toVITE_
getTypeScript
the smart tips for these user-defined environment variables prefixed with
interface ImportMetaEnv {
// 端口
readonly VITE_PORT: number
// 标题
readonly VITE_TITLE: string
// 超时时间
readonly VITE_AXIOS_TIMEOUT: string
// 删除console.log
readonly VITE_DROP_CONSOLE: boolean
// 更多环境变量...
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
在这里插入图片描述
package.json
Modifications insidescripts
, adding multiple environment modes
"scripts": {
"dev": "vite serve --mode development",
"test": "vite serve --mode test",
"prod": "vite serve --mode production",
"build:dev": "vue-tsc --noEmit && vite build --mode development",
"build:test": "vue-tsc --noEmit && vite build --mode test",
"build:prod": "vue-tsc --noEmit && vite build --mode production",
"serve": "vite preview"
}
use
Used in vue page
console.log('VITE_PORT:' + import.meta.env.VITE_PORT);
console.log('VITE_TITLE:' + import.meta.env.VITE_TITLE);
console.log('VITE_AXIOS_TIMEOUT:' + import.meta.env.VITE_AXIOS_TIMEOUT);
console.log('VITE_DROP_CONSOLE:' + import.meta.env.VITE_DROP_CONSOLE);
Trap a trap
vite.config.ts
Not available inimport.meta.env
Use loadEnv(mode, process.cwd()).VITE_PORT
, but then the TS prompt will disappear! ! !
import {
ConfigEnv, UserConfig, loadEnv} from 'vite'
export default ({
command, mode}: ConfigEnv): UserConfig => {
const isBuild = command === 'build';
/**
* mode:模式
* envDir:环境变量配置文件所在目录
* prefix:接受的环境变量前缀,默认为 VITE_,这就应证了文档中提到的内容
*/
const envConfig = loadEnv(mode, process.cwd());
const VITE_PORT = Number(envConfig.VITE_PORT);
const VITE_DROP_CONSOLE = Boolean(envConfig.VITE_DROP_CONSOLE);
const VITE_DROP_DEBUGGER = Boolean(envConfig.VITE_DROP_DEBUGGER);
return {
...
server: {
// 指定服务器端口
port: VITE_PORT,
...
}
},
build: {
...
terserOptions: {
compress: {
keep_infinity: true,
drop_console: VITE_DROP_CONSOLE,
drop_debugger: VITE_DROP_DEBUGGER
}
},
}
}
};