The environment variables of vite are on a special object. By default, only environment variables with the prefix VITE_ will be loaded.
Vite's configuration in vite.config.ts and js cannot directly obtain the desired environment variables; it cannot be obtained directly using process.env.xxx like the vue-cli scaffolding, nor can it be obtained directly using import.meta.env. xxx
Main configuration code
// vite.config.js
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path, { resolve } from "path";
export default ({ mode }) => {
const env = loadEnv(mode, process.cwd());
return defineConfig({
base: env.VITE_PROJECT_BASE,
server: {
host: "0.0.0.0", //解决 vite use--host to expose
port: 8888, //配置端口
open: true, //配置默认打开浏览器
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"), //配置@别名
},
},
plugins: [react()],
});
};