【发布时间】:2021-07-26 23:10:01
【问题描述】:
我们有一个在多个域名上运行的 react 应用,但我不确定如何在 react 中获取当前域名。
例如该应用程序在 www.example-one.com 和 www.example-two.com 上运行相同的代码,但使用 window.location 或 document.location.href 等仅返回 localhost,因为该应用程序通过 Apache 中的反向代理提供服务。
如何让它返回www.example-one.com 或www.example-two.com?
编辑:
经过更多测试后,问题是因为我在组件安装之前使用了location.href。将代码移动到componentDidMount 允许我访问域名。感谢您的帮助
【问题讨论】:
-
location.href将返回域,即使通过反向代理运行。在您的服务器上是localhost,而不是浏览器。当然,除非您通过localhost访问浏览器 -
你确定这行不通吗?你看过这个吗:stackoverflow.com/questions/51988873/…
-
您需要将您的项目分成两个不同的版本,并将它们放在具有不同配置文件的不同文件夹中。
-
感谢您的回复。我刚刚再次尝试使用
location.href导致错误location is not defined或使用window.location.href导致window is not defined并且应用程序无法启动
标签: javascript reactjs apache