[英]how to use different configuration files during deployment of react js app?
I have 3 environments and I am using Sharepoint as backend.我有 3 个环境,我使用 Sharepoint 作为后端。 Each environment has got fields which are called differently.
每个环境都有不同名称的字段。 The problem is that I can't change the internal names of these fields.
问题是我无法更改这些字段的内部名称。
I am using REST API to call SharePoint endpoints as follows:我正在使用 REST API 调用 SharePoint 端点,如下所示:
/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=field1,field2[etc]
In my react app
, I would like to achieve the following:在我的
react app
中,我想实现以下目标:
/sites/someSite
somewhere at the root of the application and access it everywhere in the application/sites/someSite
存储在应用程序根目录的某个位置,并在应用程序的任何地方访问它these fields are used in my rest api queries.这些字段用于我的 rest api 查询。
How can I configure my application so that during deployment, a configration file can be
used or some other means to assign the correct fields to each REST API call?
I can use sort of if else branch, but that does not seem realistic.我可以使用某种 if else 分支,但这似乎不现实。
Dev:开发:
/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=field1,field2,field3,field4, etc
Acc:帐户:
/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=col1,field2,col3,field4, etc
Prod:产品:
/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=fieldCol1,fieldCol2,field3,field4, etc
For this you can maintsin 3 seperate configuration/env files for your application (dev.env, ...).为此,您可以为您的应用程序维护 3 个单独的配置/env 文件(dev.env,...)。 First instlall using npm
dotenv
and env-cmd
dependencies.首先使用 npm
dotenv
和env-cmd
依赖项进行安装。 Add following script to the package json file.将以下脚本添加到 package json 文件中。
scripts": {
"start:dev": "env-cmd -f dev.env npm start"
"start:prod": "env-cmd -f prod.env npm start"
...
}
If you want to start react app using dev environment.如果您想使用开发环境启动 React 应用程序。 You can use
您可以使用
npm run start:prod
Then mentioned url like below.然后像下面提到的 url。
"/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select="+ SELECT
You can use require('dotenv').config()
to read the environments.您可以使用
require('dotenv').config()
来读取环境。
dev.env file like below. dev.env 文件如下。
SELECT = col1,col2...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.