简体   繁体   English

如何在反应应用程序中从 package.json 访问主页?

[英]How is it possible to access homepage from package.json in a react app?

I created a react app that will be served from /example-path .我创建了一个将从/example-path提供服务的 React 应用程序。 I defined it in the package.json like this:我在package.json中这样定义它:

"homepage":"/example-path"

It has worked so far, but now I would like to add routing with react-router-dom , and it incorrectly detects /example-path as the part of the URL.到目前为止它一直有效,但现在我想用react-router-dom添加路由,它错误地将/example-path检测为 URL 的一部分。

This is my code:这是我的代码:

<Router>
  <Switch>
    <Route path="/product/:id" children={<DisplayProduct />} />
  </Switch>
</Router>

Unfortunately react-router-dom tries to match the full URL /example-path/product/10 .不幸的是react-router-dom试图匹配完整的 URL /example-path/product/10 How is it possible to avoid that?如何避免这种情况? Is it possible to access the homepage variable somehow?是否有可能以某种方式访问homepage变量?


I could use <Router basename="/example-path"> , but in that way, this parameter is a duplicate.我可以使用<Router basename="/example-path"> ,但那样的话,这个参数是重复的。 I would like to define it in only one place.我只想在一个地方定义它。

The Router doesn't know anything about your package.json . Router对您的package.json It just looks at the browser location.它只是查看浏览器的位置。 You can set a basename if you use BrowserRouter :如果您使用basename ,则可以设置基本BrowserRouter

import {BrowserRouter as Router} from 'react-router';
<Router basename="/example-path">
    {/* ... */}
</Router>

Assuming you are using create-react-app , this is actually pretty straightforward:假设您使用的是create-react-app ,这实际上非常简单:

Add the following to public/index.html :将以下内容添加到public/index.html

<script>
  window.PUBLIC_URL = "%PUBLIC_URL%";
</script>

now you can reference the homepage from JavaScript!现在您可以从 JavaScript 引用主页了! Simply access it via只需通过

const myPublicUrl = window.PUBLIC_URL;

Note: The variable will be an empty string if no homePage field is set in the package.json , so we have to compensate for that:注意:如果homePage中没有设置package.json字段,则该变量将为空字符串,因此我们必须对此进行补偿:

<Router basename={window.PUBLIC_URL && window.PUBLIC_URL.length > 0 ? window.PUBLIC_URL : "/"}>
    {/* ... */}
</Router>

How is it possible to access homepage from package.json in a react app?如何在反应应用程序中从 package.json 访问主页?

We can access the URL set in package.json's homepage field using PUBLIC_URL environment variable.我们可以使用PUBLIC_URL环境变量访问 package.json 的homepage字段中设置的 URL。

We sometimes use a relative URL to deploy the application in subdirectories for example we are deploying the application in https://myhostname.com/example-path .我们有时使用相对的 URL 将应用程序部署在子目录中,例如我们将应用程序部署在https://myhostname.com/example-path中。 In order to do that, we can either set the environment variable PUBLIC_URL=/example-path for the application build or set the package.json homepage attribute to "/example-path" .为此,我们可以为应用程序构建设置环境变量PUBLIC_URL=/example-path或将 package.json 主页属性设置为"/example-path" In your case, you have set the在您的情况下,您已设置

"homepage": '/example-path/'

then the PUBLIC_URL would be set to /example-path/ .那么PUBLIC_URL将设置为/example-path/ Now you can access this environment variable anywhere in your react application.现在你可以在你的 react 应用程序的任何地方访问这个环境变量。

<Router basename={process.env.PUBLIC_URL}>
  <Switch>
    <Route path="/product/:id" children={<DisplayProduct />} />
  </Switch>
</Router>

After running the npm run build , you can check in the build/index.html that all the places where you used the %PUBLIC_URL is set to /example-path/ like below;运行npm run build后,您可以在build/index.html中检查您使用%PUBLIC_URL的所有位置都设置为/example-path/ ,如下所示;

<script type="text/javascript" src="/example-path/static/js/main.ec7f1972.js">

Since you are using create-react-app package you can use environment variable instead of "homepage":"/example-path" in your package.json :由于您使用的是create-react-app package ,因此您可以在package.json中使用环境变量而不是"homepage":"/example-path"

  1. create .env file in the root of your project.在项目的根目录中创建.env文件。

  2. add REACT_APP_HOME_PAGE='/example-path/' to the .env fileREACT_APP_HOME_PAGE='/example-path/'添加到.env文件

  3. now you can access your env variable through process.env.REACT_APP_HOME_PAGE in your app like the following:现在您可以通过应用程序中的process.env.REACT_APP_HOME_PAGE访问您的环境变量,如下所示:

     <Router basename={process.env.REACT_APP_HOME_PAGE}>

I don't know where is the location of your Router file but you can try to import it as json in src/index.js file that created by create-react-app cli app like below;我不知道你的 Router 文件的位置在哪里,但你可以尝试将它作为 json 导入到由create-react-app cli app 创建的src/index.js文件中,如下所示;

import myPackageJson from '../package.json';

or或者

import { homepage } from '../package.json';

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

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