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

时间:2019-10-03 08:55:34

标签: javascript angular

我正在尝试重定向我的Beta应用程序。 当我打开主页时,应将其重定向到另一个页面。我在window.location上使用app.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/');
    }
}

2 个答案:

答案 0 :(得分: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'] );
        }
    }

答案 1 :(得分:0)

使用路由器的NavigationStart事件并将其订阅到您的构造函数中,如下所示:

import UIKit
import WebKit



class ViewController: UIViewController {

 @IBOutlet weak var webView: WKWebView!

      let urlString: String = "https://google.com"

     override func viewDidLoad() {
         super.viewDidLoad()
         guard let url =  URL(string: urlString) else { return }
         let requestObj = URLRequest(url: url)
         webView.load(requestObj)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

} });