基于子域的Angular2路由? [英] Angular2 routing based on subdomain?
问题描述
我正在尝试基于URL中的子域在Angular2路由器中进行路由.例如,如果有人请求test.domain.com,那么他们将获得测试"路由.我无法在不设置超时延迟的情况下从ngOnInit使router.navigate正常工作,但是可以从构造函数中运行以下内容.如果有更清洁的解决方案会感兴趣吗?
I am trying to route in Angular2 Router based on the subdomain in a URL. For example if someone requests test.domain.com then they get the "test" route. I could not get router.navigate to work from ngOnInit without setting a timeout delay, but running the following from the constructor works. Would be interested if there was a cleaner solution ?
{path: 'test', component: TestComponent}
this._router.events.subscribe(event => {
if (event.constructor.name === 'NavigationEnd'
&& window.location.hostname == 'test.domain.com'
&& event.url == '/') {
console.log(event.url);
this._router.navigate(['test']);
}
});
推荐答案
您不能通过Nginx或域代理,或者通过Ingres等来做到这一点.
You can't do this by Nginx or domain proxy, or by Ingres, etc.
要解决这种情况,您可以使用不同的全局路由,并在当前域已加载代码束的情况下将它们插入到routingModule中:
To solve this case you can use different global routes and insert them to routingModule by the condition of the current domain is loaded code bundle:
您将用重复的代码,双重应用程序解决问题,而仅用一个应用程序内的现有组件进行另一种路由模型.
You will solve problem with duplicates of code, double applications, but only another model of routing with existing components inside one app.
// app.routing.ts
const TEST_routes: Routes = [
{
path: '',
component: TestPageComponent,
},
];
const PROJECT_routes: Routes = [
{
/// all needed routes of the whole main project
},
];
const isCurrentDomainTest: boolean =
(window.location.hostname === 'test.localhost') || // local
(window.location.hostname === 'test.yourdomain.com'); // prod
imports: [
RouterModule.forRoot(
isCurrentDomainTest ? TEST_routes : PROJECT_routes)
]
这篇关于基于子域的Angular2路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!