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']);
}
});
See Question&Answers more detail:os