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.