Javascript 如何在不首先加载组件的情况下重定向主页?

Javascript 如何在不首先加载组件的情况下重定向主页?,javascript,angular,Javascript,Angular,我正在尝试重定向我的测试版应用程序。 当我打开主页时,它应该被重定向到另一个页面。我正在使用app.component上的window.location,但它开始先加载组件,然后重定向,所以看起来有点奇怪 ngOnInit():void{ this.location=String(window.location); console.log('this.location'); 如果(this.location=='http://localhost:4200/home') { window.loca

我正在尝试重定向我的测试版应用程序。 当我打开主页时,它应该被重定向到另一个页面。我正在使用
app.component
上的
window.location
,但它开始先加载组件,然后重定向,所以看起来有点奇怪

ngOnInit():void{
this.location=String(window.location);
console.log('this.location');
如果(this.location=='http://localhost:4200/home') {
window.location.replace('https://www.google.com/');
}
}

使用路由器的NavigationStart事件并在构造函数中订阅它,如下所示:

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/');
         }
     }
}
});

这就是它的用途。如果用户未经身份验证,它应该重定向到另一个页面吗?或者在所有情况下,它应该重定向到另一个页面吗?我认为您正在寻找loadChildren-因此只加载最小的组件-
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/');
         }
     }