小编Lor*_*aco的帖子

如何将 dotenv 与 vite/petite vue 一起使用?

如何.env在项目中使用变量petite-vue?运行时yarn vite给我错误:process is not defined

文件结构是这样的:

Root
|_index.html
|_vue-index.ts
Run Code Online (Sandbox Code Playgroud)

我已经导入了 vue 脚本,index.html并且console.log('Hello')连接到按钮之类的东西工作正常,所以这不是导入脚本的问题。但是,当我尝试时,console.log(process.env.URL)它会返回该错误,并且 @click 不会执行任何操作。

Root
|_index.html
|_vue-index.ts
Run Code Online (Sandbox Code Playgroud)
# .env
URL=http://example.com
Run Code Online (Sandbox Code Playgroud)

问题似乎是import.meta.env.自然而然地出现的。我的问题是,对于 TS,如果不将tsconfig.jsonmoule 选项更改为esnext或其他,则不允许这样做。

有人可以解释我如何使用它吗?如果我运行ts-node正常的话。

// vue-index.ts
import { createApp } from 'petite-vue';
import 'dotenv/config';

createApp({
  print() {
   console.log(process.env.API_URL);
  },
}).mount('#app');
Run Code Online (Sandbox Code Playgroud)

environment-variables typescript vue.js

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

environment-variables ×1

typescript ×1

vue.js ×1