如何在 netlify 上设置环境变量?
我有一个 netlify React 应用程序。它连接到我的 github。我正在使用 emailjs 来接收来自访问我的应用程序的人的消息。
emailjs 处理三个 id 'SERVICE_ID'、'TEMPLATE_ID' 和 'USER_ID'。但我不想在我的组件 js 文件中公开使用它们。
驱动功能
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 have a netlify react app. which is connected to my github. I'm using emailjs for receiving the messages from whoever reaches to my app.
emailjs deals with three ids 'SERVICE_ID', 'TEMPLATE_ID' and 'USER_ID'. But I don't wanna use them openly in my component js file.
Driver 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);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您指的是环境变量,为了在本地进行测试,它会根据您用于创建应用程序的堆栈而有所不同,如果您使用react create app,您可以在项目的根目录中创建一个 .env 文件,填充值
不要忘记从 git 中排除此文件,以避免在您的存储库中推送机密。为此,请将其添加到您的 .gitignore
之后,您可以使用 进程调用代码中的变量.env 像这样:
process.env.REACT_APP_SERVICE_ID
现在修改代码:
要使其在 netlify 中工作,您必须在 netlify 项目中添加变量,请按照本节操作为此: https://docs.netlify.com/configure -builds/environment-variables/#declare-variables
正如您所指出的,我添加了前缀 REACT_APP_,这是因为 React create app 会执行以下操作:
如果您使用 gatsby 或 nextjs,环境变量命名约定可能会改变,所以请注意这一点。
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
Don't forget to exclude this file from git, to avoid pushing secrets in your repo. to do that add this to your .gitignore
After that you can call the variables in your code using the process.env like this:
process.env.REACT_APP_SERVICE_ID
Now modify the code:
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
As you noted I added the prefix REACT_APP_, this is because react create app does this:
If you are using gatsby or nextjs the env variables naming convention might change so please be aware of that.
您可以在 netlify 上设置环境变量。请检查下面的图片。
You can set env variables on netlify. Please have a check the below images.
检查 向 React 应用添加环境变量
您可以创建一个 < code>.env 在你的根目录中,并在其中添加你的密钥、api 端点...。
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.