The redirectTo
property isn't working in my Angular 2 app. I have the following routes in my app.routing.ts
:
const routes: Routes = [
{ path: '', redirectTo: '/page/1', pathMatch: 'full' },
{ path: 'page', loadChildren: 'app/modules/page/page.module#PageModule' }
]
export const routing = RouterModule.forRoot(routes);
Then, in my page.routing.ts
, I have the following:
const pageRoutes: Routes = [
{ path: ':id', component: PageComponent, canActivate: [LoginGuard] }
];
export const pageRouting = RouterModule.forChild(pageRoutes);
Every time I access the home page it displays the LoginComponent
for a second, then it disappears. However, it should redirect to the PageComponent
.
Why isn't that happening? Why the LoginComponent
is being loaded (even if it's only for a brief second) if the user is already logged in?
Here's my LoginGuard
:
@Injectable()
export class LoginGuard implements CanActivate {
constructor(private af: AngularFire, private router: Router) {}
canActivate(): Observable<boolean> {
return this.af.auth.map(auth => {
if (auth === null) {
this.router.navigate(['/login']);
return false;
} else {
return true;
}
}).first();
}
}
EDIT: Temporarily, I changed the LoginComponent
to redirect to the PageComponent
if a user is logged in. I still wonder, though, why redirectTo
isn't working.