Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to create an external redirect, but to make all routes consistent I think it would be nice to do everything(including external redirects) under Router States configuration.

so:

const appRoutes: Routes = [   {path: '', component: HomeComponent},   {path: 'first', component: FirstComponent},   {path: 'second', component: SecondComponent},   {path: 'external-link', /*would like to have redirect here*/}       ]; 

UPD: and I don't want to use empty component for this case like @koningdavid suggested. This solution looks really weird for me. It should be something really easy to implement for such case, without virtual components.

like image 550
Yaroslav Polubedov Avatar asked Oct 20 '16 09:10

Yaroslav Polubedov


People also ask

How do I change the URL in an Angular application without reloading the route?

In order to update the route without a reload of the entire component (page) I had to resort to plain ol' JavaScripts replaceState function. This way, the current route is substituted for a new one. If you want to keep the history, you can use pushState.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

Which Angular package is used to route to URL?

Generate an application with routing enabledlink The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is an NgModule where you can configure your routes.

Can we give routerLink to div?

Yes it can be attached to div tag, your route is probably wrong try add / in front of route.


1 Answers

You can achieve what you want with a trick using the resolve option of a route. Resolve is some data value that Angular2 will obtain for the route to be initialized. More details you can find here in the official documentation.

I have tried this approach and it does work. Example:

Add this to the provider section (plus import the required classes from Routing)

@NgModule({     providers: [         {             provide: 'externalUrlRedirectResolver',             useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>             {                 window.location.href = (route.data as any).externalUrl;             }         }     ] }) 

Then you can define your route like this:

{         path: 'test',         component: AnyRandomComponent,         resolve: {             url: 'externalUrlRedirectResolver'         },         data: {             externalUrl: 'http://www.google.com'         }     } 

This will redirect to the external URL. It's a bit of a hackish way really. I tried to achieve the result without using the component at all, but you have to use either redirectTo or component or children or loadChildren. redirectTo won't trigger the resolve and I am not sure about children, though you can experiment.

You can implement it in a nice class rather than direct function in provider. More info in the documentation (see reference above).

P.S. I would really rather use a redirect component myself I think. Just use the trick with the data and getting the state from the router with externalUrl to get this as a parameter.

like image 70
Ilya Chernomordik Avatar answered Oct 15 '22 00:10

Ilya Chernomordik