10

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
2
  • did you find a solution for that Commented Apr 3, 2018 at 15:17
  • 3
    Use Angular's CanActivate on the route and put your redirect logic there. It's essentially route middleware.
    – David Alsh
    Commented Apr 3, 2018 at 23:32

1 Answer 1

9

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.