Angular 2 router - When logged out and a user tries to navigate to a logged in page I need the app.ts to redirect.
I'm using typescript with angular 2.
For some reason the redirect works for some pages. But when I have code in the constructor it hits it. I want to redirect the user to homepage straight away if not logged in.
I'm checking if they are logged in and then I do this code if they are not logged in in the app.ts file:
this.router.navigate(['Home']);
This works for basic pages but when I try access my search page for example I get console errors because its accessing the components constructor.
This is my route config in the app.ts file:
@RouteConfig([
{ path: '/', component: Home, as: 'Home'},
{ path: '/home', component: Home, as: 'Home' },
{ path: '/login', component: Login, as: 'Login' },
{ path: '/register/:id', component: Register, as: 'Register' },
{ path: '/forgotpassword', component: ForgotPassword, as: 'ForgotPassword' },
{ path: '/dashboard', component: Dashboard, as: 'Dashboard' },
{ path: '/search', component: SearchJobs, as: 'Search' },
{path:'/**', redirectTo: ['Home']}
])