How can I convince the Angular 4 router to disable direct browser navigation in my application?
I looked at the hooks of the router CanActivateand CanDeactivateimplemented them, but it CanDeactivatedoes not start at all when using direct browser navigation.
I can use CanActivateto prevent the URL, but only after restarting the application, which takes time and unwanted changes in the browser window.
I want to catch direct browser navigation before the application restarts.
In case this helps: I want to prevent browser navigation altogether, so I don't need a solution that allows certain URLs and disables others.
Here is my router definition:
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent, canActivate: [RouteGuardService] },
{ path: 'users', component: UsersMainComponent, canActivate: [RouteGuardService] },
{ path: 'vendors', component: VendorMainComponent, canActivate: [RouteGuardService] },
{ path: 'invoices', component: InvoicesMainComponent, canActivate: [RouteGuardService] },
{ path: 'offers' , component: EsdMainComponent, canActivate: [RouteGuardService] },
{ path: 'settings' , component: SettingsMainComponent, canActivate: [RouteGuardService] },
{ path: 'callback' , component: CallbackComponent },
{ path: 'createvendor' , component: CreateVendorsComponent, canActivate: [RouteGuardService] },
{ path: 'customerdashboard' , component: CustomerDashboardComponent, canActivate: [RouteGuardService] },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [RouteGuardService]
})
export class AppRoutingModule { }