Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a function in the router's "redirectTo" to determine where to redirect prevents aot

I am using a function to determine where the user should be redirected on the loading of the site. Like so:

{   path : '', redirectTo: redirector(),     pathMatch: 'full' }

redirector() returns a route (string) eg: 'home/browse'

This causes the following issue with ng build --prod --aot

Uncaught Error: Invalid configuration of route ''. One of the 
following must be provided: component, redirectTo, children or 
loadChildren
like image 634
David Alsh Avatar asked Sep 01 '17 01:09

David Alsh


People also ask

Which is used to redirect to error component?

navigate['error']; });

How do you restrict a user to access a particular page using a direct URL in Angular?

You can import router in the constructor of a guard. This router instance will have the current URL. ActivatedRouteSnapshot and RouterStateSnapshot in canActivate will contain the URL that the user is attempting to access. The below example prevents users from directly accessing a route from an outside page.


1 Answers

Here you can see a github issue that also is related to angular guard (conditional) redirection.

The example solution from the github issue is (also mentioned by @David Alsh in the comments earlier):
{path: '', pathMatch: 'full', children: [], canActivate: [RedirectGuard]}

And then in the RedirectGuard's canActivate method you could use: this.router.navigate(), where this.router is the Router from '@angular/router': https://angular.io/guide/router, in order to execute the redirect.

Example use of this.router.navigate():
this.router.navigate(['/heroes']);

You can find even more details in this stackoverflow question.

Good luck!

like image 91
Martin Patsov Avatar answered Oct 06 '22 16:10

Martin Patsov