路由基于从URL的主机名的REACT REACT组件
我想根据主机名加载React组件,例如,假设我们有React Application“ Frontend”和URL访问此应用程序是< base_url>/test/test/login 或
< base_url>/test/home
。 现在,我想从2个不同的主机名访问此应用。
test.abc.com/test/login,test.abc.com/test/home
test.xyz.com/test/login,test.xyz.com/test/home
现在,如果用户使用#1,则仅显示test.abc.com
的登录/主页/注销页面,并且对于#2登录/home/home/logout页面不同。 基本上,我需要自定义基于UI的O主机名。
谁能告诉我,我该如何实现这种情况,或者还有其他方法可以做到这一点?
I want to load React Component based on host name , Example , let say We have React application 'Frontend' and URL to access this app is <base_url>/test/login
or <base_url>/test/home
.
Now I want to access this app from 2 different host name.
test.abc.com/test/login , test.abc.com/test/home
test.xyz.com/test/login , test.xyz.com/test/home
Now If user use #1 then it will show the login/home/logout page for test.abc.com
only and for #2 login/home/logout page is different.
Basically I need to customized the UI based o hostname.
Can anyone tell me , How can I achieve this scenario or is there any other way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
window.location.hostname
获取主机名并在'中拆分。Use
window.location.hostname
to get the hostname and split at '.'测试
location.hostname
的
从2(如问题中指定)到100是门柱移动,但是……
由于包括100个不同的登录组件会膨胀应用程序,因此我会在构建时间进行选择。细节将取决于您使用哪种工具链来构建生产应用程序。
例如,如果您使用的是WebPack,则可以执行类似的操作:
这将要求您在构建前设置
targethostname
环境变量。location.hostname
For example:
From a comment on another answer:
Going from 2 (as specified in the question) to 100 is something of a goalpost move, but…
Since including 100 different Login components would bloat the application, I would do the selection at build time. The specifics would depend on which toolchain you were using to build your production app.
For example, if you were using Webpack you could do something like:
This would require that you set the
TARGETHOSTNAME
environment variable before building.使用
window.location.hostname
和使用效果
以实现所需的行为。基本上,请检查使用
内部的URL,并使用recroCtRouterdom库或类似的内容重定向。Use
window.location.hostname
anduseEffect
to achieve desired behaviour. Basically check the url insideuseEffect
and redirect using ReactRouterDOM library or something similar.获取带有JavaScript函数的主机名
location.hostname;
如果主机名是test.abc.com重定向到test.abc.com/logout其他重定向到test.xyz.com/logout
get the hostname with the javascript function
location.hostname;
if the hostname is test.abc.com redirect to test.abc.com/logout else redirect to test.xyz.com/logout