Trying to create my redirect route in my angular2 application.
But my problem is that when someone enters an invalid path, like "nopath", then the user is redirected to "HomeComponent", but url still saves "/ # / nopath"
I want the redirectTo route to change the url! How should I achieve this?
Should I put an if in my HomeComponent constructor, which checks the current URL and changes its route to homcomomponent? Or am I missing something?
Routes
const routes: Routes = [
{ path: '', pathMatch: 'full', component: HomeComponent },
{ path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] },
{ path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] },
{ path: 'users', component: UserComponent, canActivate: [AuthGuard] },
{ path: '**', redirectTo: '' }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes, {useHash: true});
EDIT:
Tried this, but I am not getting redirect to home component
const routes: Routes = [
{ path: '', pathMatch: 'full' , redirectTo: 'home' },
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] },
{ path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] },
{ path: 'users', component: UserComponent, canActivate: [AuthGuard] },
{ path: '**', redirectTo: '' }
];