无法在Nuxt.js中读取环境变量?

huangapple go评论55阅读模式
英文:

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 变量,你需要执行以下步骤:

  1. 通过 npm 或 yarn 安装 @nuxtjs/dotenv
npm install --save-dev @nuxtjs/dotenv
  1. 打开你项目根目录下的 nuxt.config.js 文件,并添加以下代码:
export default {
  // 其他 Nuxt 3 配置选项

  vite: {
    plugins: [
      require('dotenv').config()
    ]
  }
};
  1. 现在你可以在任何组件或 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:

  1. Install @nuxtjs/dotenv via npm or yarn
npm install --save-dev @nuxtjs/dotenv
  1. 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(&#39;dotenv&#39;).config()
    ]
  }
};
  1. Now you can use .env variables in any component or js files:

Using in template:

&lt;template&gt;
  &lt;div&gt;
    &lt;p&gt;API Base URL: {{ $env.API_BASE_URL }}&lt;/p&gt;
    &lt;p&gt;API Key: {{ $env.API_KEY }}&lt;/p&gt;
  &lt;/div&gt;
&lt;/template&gt;

Using in script:

&lt;script&gt;
export default {
  mounted() {
    console.log(&#39;API Base URL:&#39;, this.$env.API_BASE_URL);
    console.log(&#39;API Key:&#39;, this.$env.API_KEY);
  }
};
&lt;/script&gt;

More info about @nuxtjs/dotenv package: https://www.npmjs.com/package/@nuxtjs/dotenv

huangapple
  • 本文由 发表于 2023年6月8日 20:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定