如何使用Nuxt.JS应用访问Heroku环境变量 [英] How to access Heroku environment variables with Nuxt.JS app

查看:217
本文介绍了如何使用Nuxt.JS应用访问Heroku环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在Heroku上部署了我的应用程序,并在我的应用程序启动时检查了一个配置文件,并在其中访问了我在Heroku API_KEY上创建的用于定义Firebase配置的配置变量:

I have deployed my app on Heroku and on the start of my app I check a config file and in there I want to access a config var I have created on Heroku API_KEY to define my Firebase config:

module.exports = {
  fireConfig: {
    apiKey: process.env.API_KEY,
    authDomain: "my-app.firebaseapp.com",
    databaseURL: "https://my-app.firebaseio.com",
    projectId: "my-project-id",
    storageBucket: "my-app.appspot.com",
    messagingSenderId: "my-messaging-sender-id"
    }
};

process.env.API_KEYundefined.我应该使用其他方式访问它吗?

this process.env.API_KEY is undefined. Should I use a different way to access it?

推荐答案

您可以在以下位置定义环境变量您的nuxt.config.js文件,例如

You can define environment variables in your nuxt.config.js file, e.g.

export default {
  env: {
    firebaseApiKey: process.env.API_KEY || 'default value'
  }
}

在这里,我们使用API_KEY环境变量(如果可用),并将其分配给firebaseApiKey.

Here we use the API_KEY environment variable, if it's available, and assign that to firebaseApiKey.

如果未设置该环境变量,例如也许在开发中(如果需要,也可以在此处进行设置),我们退回到'default value'.请不要将您的真实API密钥放入'default value'.您可以在此处使用单独的一次性Firebase帐户密钥,也可以忽略它(直接删除|| 'default value'),并依赖于设置的环境变量.

If that environment variable isn't set, e.g. maybe in development (you can set it there too if you want), we fall back to 'default value'. Please don't put your real API key in 'default value'. You could use a separate throwaway Firebase account key here or just omit it (take || 'default value' right out) and rely on the environment variable being set.

这些将在构建时进行处理,然后使其可用使用您给他们的名字,例如

These will be processed at build time and then made available using the name you give them, e.g.

module.exports = {
  fireConfig: {
    apiKey: process.env.firebaseApiKey,  # Not API_KEY
    // ...
};

这篇关于如何使用Nuxt.JS应用访问Heroku环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆