gpt4 book ai didi

vuejs3 - 如何在 vite.config.js 中使用 Vite 环境变量?

转载 作者:行者123 更新时间:2023-12-04 00:51:13 26 4
gpt4 key购买 nike

在我的 Vite 项目中使用以下 .env:

# To prevent accidentally leaking env variables to the client, only
# variables prefixed with VITE_ are exposed to your Vite-processed code

VITE_NAME=Wheatgrass
VITE_PORT=8080
如何在我的 VITE_PORT 中使用 vite.config.js

最佳答案

您可以加载 app level 环境变量并将它们添加到 Node level 环境变量中:

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';


export default ({ mode }) => {
process.env = {...process.env, ...loadEnv(mode, process.cwd())};

// import.meta.env.VITE_NAME available here with: process.env.VITE_NAME
// import.meta.env.VITE_PORT available here with: process.env.VITE_PORT

return defineConfig({
plugins: [vue()],

server: {
port: process.env.VITE_PORT,
},
});
}

关于vuejs3 - 如何在 vite.config.js 中使用 Vite 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66389043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com