简体   繁体   English

如何在 netlify 上设置环境变量?

[英]How can I set environment variables on netlify?

I have a netlify react app.我有一个 netlify 反应应用程序。 which is connected to my github.它连接到我的 github。 I'm using emailjs for receiving the messages from whoever reaches to my app.我正在使用 emailjs 来接收来自我的应用程序的任何人的消息。

emailjs deals with three ids 'SERVICE_ID', 'TEMPLATE_ID' and 'USER_ID'. emailjs 处理三个 id 'SERVICE_ID'、'TEMPLATE_ID' 和 'USER_ID'。 But I don't wanna use them openly in my component js file.但我不想在我的组件 js 文件中公开使用它们。

Driver Function驱动器 Function

  function sendEmail(e) {
    e.preventDefault();    //This is important, i'm not sure why, but the email won't send without it

    emailjs.sendForm(SERVICE_ID, TEMPLATE_ID, e.target, USER_ID)
      .then((result) => {
          window.location.reload()  //This is if you still want the page to reload (since e.preventDefault() cancelled that behavior)
      }, (error) => {
          console.log(error.text);
      });
  }

I think you're referring to environment variables, in order to test that out locally it will vary per the stack you use for creating the app, if you use react create app you can create a.env file in the root of your project and populate the values我认为您指的是环境变量,为了在本地测试它会根据您用于创建应用程序的堆栈而有所不同,如果您使用 react create app 您可以在项目的根目录中创建一个 .env 文件,并且填充值

REACT_APP_SERVICE_ID ="your_value"
REACT_APP_TEMPLATE_ID ="your_value"
REACT_APP_USER_ID ="your_value"

Don't forget to exclude this file from git, to avoid pushing secrets in your repo.不要忘记将此文件从 git 中排除,以避免在您的存储库中推送秘密。 to do that add this to your.gitignore为此将其添加到 your.gitignore

.env

After that you can call the variables in your code using the process.env like this:之后,您可以使用process.env调用代码中的变量,如下所示:

process.env.REACT_APP_SERVICE_ID

Now modify the code:现在修改代码:

  function sendEmail(e) {
   // Your code

   emailjs.sendForm(process.env.REACT_APP_SERVICE_ID, process.env.REACT_APP_TEMPLATE_ID, e.target, process.env.REACT_APP_USER_ID)
   // your promise
  }

To make that work in netlify you would have to add the variable in your netlify project, follow this section to do that: https://docs.netlify.com/configure-builds/environment-variables/#declare-variables要在 netlify 中工作,您必须在 netlify 项目中添加变量,请按照本节操作: https://docs.netlify.com/configure-builds/environment-variables/#declare-variables

As you noted I added the prefix REACT_APP_, this is because react create app does this:正如你所说,我添加了前缀 REACT_APP_,这是因为 react create app 这样做:

Note: You must create custom environment variables beginning with REACT_APP_.注意:您必须创建以 REACT_APP_ 开头的自定义环境变量。 Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name.除了 NODE_ENV 之外的任何其他变量都将被忽略,以避免在机器上意外暴露可能具有相同名称的私钥。 Changing any environment variables will require you to restart the development server if it is running.更改任何环境变量都需要您重新启动正在运行的开发服务器。

If you are using gatsby or nextjs the env variables naming convention might change so please be aware of that.如果您使用的是 gatsby 或 nextjs,环境变量命名约定可能会发生变化,因此请注意这一点。

Check Adding env variables to react app检查添加环境变量以响应应用程序

You can create a .env in your root dir and add your keys, api end points,... inside of it.您可以在根目录中创建一个.env并在其中添加您的密钥 api 端点,...。

You can set env variables on netlify.您可以在 netlify 上设置环境变量。 Please have a check the below images.请检查以下图片。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM