1

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

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

2 回答 2

1

使用路由器的 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/');
         }
     }

} });

于 2019-10-03T09:19:19.917 回答
0
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'] );
        }
    }
于 2019-10-03T09:16:07.520 回答