通过import.meta打印出来的是这样的
console.log(import.meta)
// 打印出的对像结果如下
{
resolve: {...},
url: ''
}
如果能打印到有url,很奇怪,没有env属性,env其实是存在,不知道是不是因为env不是import.meta的内置属性还是,env这个属性是被隐藏了,直接打印import.meta是没有env对象的,直接打印import.meta.env就行
console.log(import.meta.env) // 会有惊喜
使用vite构建工具,配置.env文件时,定义的环境变量必须以VITE_开头才能被vite识别,当然如果不想以VITE_开头,vite.config.js需要配置envPrefix或者使用define来定义前缀
// vite.config.js
export default {
envPrefix: 'CUSTOM_'
}
// 第二种方法
// vite.config.js
export default {
define: {
'process.env.CUSTOM_VAR': JSON.stringify(process.env.CUSTOM_VAR)
}
}