Categories
Angular Answers

How to redirect to an external URL from Angular route without using component?

Spread the love

Sometimes, we want to redirect to an external URL from Angular route without using component.

In this article, we’ll look at how to redirect to an external URL from Angular route without using component.

How to redirect to an external URL from Angular route without using component?

To redirect to an external URL from Angular route without using component, we can set window.location.href to a value in our code.

For instance, we write

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "redirect",
  template: "redirecting...",
})
export class RedirectComponent implements OnInit {
  constructor() {}

  ngOnInit() {
    window.location.href = "http://www.example.com";
  }
}

to create the RedirectComponent that redirects to http://www.example.com with

window.location.href = "http://www.example.com"

when the component mounts.

Conclusion

To redirect to an external URL from Angular route without using component, we can set window.location.href to a value in our code.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.