本文介绍了您如何在HTML< script src =””中引用process.env变量, ?反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个react应用,正在使用dotenv-webpack来管理我的API密钥。

I have a react app and am using dotenv-webpack to manage my API keys.

我有:
-使用API​​密钥创建了一个.env并对其进行了gitignored。
-必需并将dotenv作为插件添加到webpack.config.js中。

I have:- created a .env with the API keys and gitignored it.- required and added dotenv as a plugin in webpack.config.js.

在那之后,我已经能够引用a键.js文件通过使用process.env.api_key。但是我在尝试在脚本标签的index.html中引用它时遇到问题。

After that, I've been able to reference one of the keys in a .js file by using process.env.api_key. But I am having problems trying to reference it in index.html in the script tag.

index.html:

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/style/style.css">
    <link rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/48938155eb24b4ccdde09426066869504c6dab3c/dist/css/bootstrap.min.css">
    <script src="https://maps.googleapis.com/maps/api/js?key=process.env.GOOGLEMAP_API_KEY"></script>
  </head>
  <body>
    <div class="container"></div>
  </body>
  <script src="/bundle.js"></script>
</html>

如何在此处引用process.env.API_key?

How do I reference the process.env.API_key here?

<script src="https://maps.googleapis.com/maps/api/js?key=[API_KEY]"></script>

我尝试使用在.js文件中起作用的反引号,例如 $ {API_KEY} ,但这不适用于.html文件。

I have tried using backquotes that work in .js file, like so ${API_KEY}, but that does not work in the .html file.

推荐答案

我能够使用发件人发布的代码来使其工作上面的注释中的bigmugcup。我没有修改webpack.config.js文件。

I was able to get this to work using the code posted by bigmugcup in the comments above. I did not modify the webpack.config.js file.

这篇关于您如何在HTML&lt; script src =””中引用process.env变量, ?反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:00