英文:
Can not read env varible in nuxtjs?
问题
I'm using nuxtjs 3, i'm trying to setup an axios instance so i create a file in .\plugins folder, then i put my config file in another folder, this file export an object which i will use to create my axios instance. I setup like this because sometime in the future i can reuse it. In my plugin i tried to create an axios instance, but i realize that i got undefined baseUrl.
config
export default {
baseURL: process.env.BASE_URL,
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
}
import axios from "axios";
import config from "@/config/axios-config.js";
export default defineNuxtPlugin(() => {
return {
provide: {
hello: () => {
const instance = axios.create(config);
console.log(config);
return instance;
},
},
};
});
英文:
I'm using nuxtjs 3, i'm trying to setup an axios instance so i create a file in .\plugins folder, then i put my config file in another folder, this file export an object which i will use to create my axios instance. I setup like this because sometime in the future i can reuse it. In my plugin i tried to create an axios instance, but i realize that i got undefined baseUrl.
I'm trying to setup an nuxtjs 3 base code for my project. Can you suggest me any public git repo example for it
config
export default {
baseURL: process.env.BASE_URL,
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
}
import axios from "axios";
import config from "@/config/axios-config.js";
export default defineNuxtPlugin(() => {
return {
provide: {
hello: () => {
const instance = axios.create(config);
console.log(config);
return instance;
},
},
};
});
答案1
得分: 0
这是一个有趣的问题,我曾经遇到过类似的情况,并发现我不得不在Vite中执行以下操作,这可能也适用于NuxtJS底层使用的内容?
在配置文件中使用 process.env.BASE_URL
,基本上在与构建或服务器服务运行时相关的任何文件中都要使用。
在实际的应用程序项目代码中使用 import.meta.env.BASE_URL
。
英文:
This is a funny one, I had a similar issue and found I was having to do the following with Vite that might apply to what NuxtJS uses under its hood too?
Use process.env.BASE_URL
in configs, basically any files involved with the build or server service runtimes.
Use import.meta.env.BASE_URL
when in actual App project code.
答案2
得分: 0
Sure, here is the translated content:
如果你想在 Nuxt 3 应用中使用 .env
变量,你需要执行以下步骤:
- 通过 npm 或 yarn 安装
@nuxtjs/dotenv
:
npm install --save-dev @nuxtjs/dotenv
- 打开你项目根目录下的
nuxt.config.js
文件,并添加以下代码:
export default {
// 其他 Nuxt 3 配置选项
vite: {
plugins: [
require('dotenv').config()
]
}
};
- 现在你可以在任何组件或 js 文件中使用
.env
变量:
在模板中使用:
<template>
<div>
<p>API 基础 URL:{{ $env.API_BASE_URL }}</p>
<p>API 密钥:{{ $env.API_KEY }}</p>
</div>
</template>
在脚本中使用:
<script>
export default {
mounted() {
console.log('API 基础 URL:', this.$env.API_BASE_URL);
console.log('API 密钥:', this.$env.API_KEY);
}
};
</script>
有关 @nuxtjs/dotenv
包的更多信息,请访问:https://www.npmjs.com/package/@nuxtjs/dotenv
英文:
if you want use .env
variables in nuxt 3 app you need to do that:
- Install
@nuxtjs/dotenv
via npm or yarn
npm install --save-dev @nuxtjs/dotenv
- Open the
nuxt.config.js
file in your project's root directory and add the following code:
export default {
// Other Nuxt 3 configuration options
vite: {
plugins: [
require('dotenv').config()
]
}
};
- Now you can use
.env
variables in any component or js files:
Using in template:
<template>
<div>
<p>API Base URL: {{ $env.API_BASE_URL }}</p>
<p>API Key: {{ $env.API_KEY }}</p>
</div>
</template>
Using in script:
<script>
export default {
mounted() {
console.log('API Base URL:', this.$env.API_BASE_URL);
console.log('API Key:', this.$env.API_KEY);
}
};
</script>
More info about @nuxtjs/dotenv
package: https://www.npmjs.com/package/@nuxtjs/dotenv
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论