Reputation: 153
I am trying to redirect my beta app.
When I open the home page, it should be redirected to another page. I am using window.location
on app.component
but it's starting to load the components first and then redirecting so it looks kind of weird.
ngOnInit(): void {
this.location = String(window.location);
console.log('this.location);
if (this.location === 'http://localhost:4200/home') {
window.location.replace('https://www.google.com/');
}
}
Upvotes: 1
Views: 1034
Reputation: 736
Use NavigationStart event of router and subscribe it in your constructor like below :
constructor(router:Router) {
router.events.subscribe(event => {
if(event instanceof NavigationStart) { // import { NavigationStart } from '@angular/router';
if (this.location === 'http://localhost:4200/home') {
window.location.replace('https://www.google.com/');
}
}
} });
Upvotes: 1
Reputation: 1298
import { Router } from '@angular/router';
@Component( {
selector: 'app-viewtables',
templateUrl: './viewtables.component.html',
styleUrls: ['./viewtables.component.css']
} )
export class Home{
constructor(private router: Router ) {
this.router.navigate( ['/secondpage'] );
}
}
Upvotes: 0