问题描述
我有一个 Asp.Net Core 应用程序。它使用 ReactJS。在 ./ClientApp/public 我有一个名为 configuration.js 的 JS,其中包含:
var configs = {
"apiUrl": "https://localhost:44356"
}
... 然后在 index.html 的 body 部分:
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script src="configuration.js"></script>
<!--
This HTML file is a template.
If you open it directly in the browser,you will see an empty page.
You can add webfonts,Meta tags,or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development,run `npm start` or `yarn start`.
To create a production bundle,use `npm run build` or `yarn build`.
-->
</body>
...(我在 webpack.config.js 中导入它)所以现在 react 可以使用 Config.apiUrl。我想在 configuration.js 中添加一个版本变量。并使用来自 Call .NET methods from JavaScript functions in ASP.NET Core Blazor 的 DotNet.invokeMethod
之类的东西在 javascript 中读取 .NET 程序集版本。任何帮助将不胜感激。
解决方法
放弃获取汇编版本...
阅读 SO 问题 Get version number from package.json in React Redux (create-react-app) 后,我将 REACT_APP_VERSION=$npm_package_version
添加到我的 .env。现在,在我的 React 组件中,我可以访问 process.env.REACT_APP_VERSION。我用 npm version patch
、npm version minor
或 npm version major
撞了我的版本。问题解决了。