6

I am trying to get a route to an external URL. Below is my example so far.

The error I am getting is:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'https:/google.com'

Error: Cannot match any routes. URL Segment: 'https:/google.com'

here is my code

ViolatorLink() {
    this.router.navigate(['https://www.google.com/'], {
        queryParams: {mid: this.mid}
      }
    );
  }
Community
  • 1
  • 1
Alex D
  • 788
  • 2
  • 11
  • 33
  • You can refer below link in detail [Route to external URL](https://stackoverflow.com/questions/40150393/how-to-redirect-to-an-external-url-from-angular2-route-without-using-component/40395382) – Nilesh Sutar Jun 14 '18 at 17:15

3 Answers3

9

Ideally, you should use the window.location.href = "https://www.google.com"; for your external URL calls because this.router.navigate looks the URL in angular routings.

Your function should be like:

ViolatorLink() {
    window.location.href = "https://www.google.com";
  }
Rohit.007
  • 3,414
  • 2
  • 21
  • 33
5

You need to use instead of this.router.navigate

window.location.href = "https://www.google.com";

this.router.navigate will navigate to only the elements configured in your router config module. So you are getting the error above.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
-4
<a [href]="https://www.yourExternalURL.com">

I am typing extra characters because SO now needs at least 30 characters in an answer.

Andrew Koper
  • 6,481
  • 6
  • 42
  • 50
  • 1
    SO needs 30 characters so you'd explain your answer instead of just posting code(as you did). Also this is only helpful if you want to redirect to an external URL directly from the view. If instead you want to redirect from the component then this doesn't help. – Vinc Aug 25 '20 at 10:48