设置不带create-react-app的ENV变量 [英] Setting up ENV Variables Without create-react-app

查看:54
本文介绍了设置不带create-react-app的ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您的React项目不是使用create-react-app构建且没有后端时,设置ENV变量以在您的React项目中工作的过程是什么?

What would be the process of setting up ENV variables to work in your react project when your react project isn't built using create-react-app, and has no backend?

推荐答案

找到了答案.引用这篇文章>阿米努卡诺(Aminu Kano).

Found the answer. Quoted from this post by Aminu Kano.

Webpack用户

如果您使用的是webpack,则可以安装和使用dotenv-webpack插件,请按照以下步骤操作:

If you are using webpack, you can install and use dotenv-webpack plugin, to do that follow steps below:

安装软件包

纱线添加dotenv-webpack或npm我dotenv-webpack

// .env 
API_KEY='my secret api key' Add it to webpack.config.js file

//webpack.config.js const Dotenv = require('dotenv-webpack');

// webpack.config.js const Dotenv = require('dotenv-webpack');

// webpack.config.js
const Dotenv = require('dotenv-webpack');

module.exports = {
  ...
  plugins: [
    new Dotenv()
  ]
  ...
};

在您的代码中将其用作

 process.env.API_KEY

有关更多信息和配置信息,请访问此处

For more information and configuration information, visit here

这篇关于设置不带create-react-app的ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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