如何说服 Angular 4 路由器在我的应用程序中禁用直接浏览器导航?
我已经查看了路由器挂钩CanActivate
并CanDeactivate
实现了它们,但CanDeactivate
在使用直接浏览器导航时根本不会触发。
我可以CanActivate
用来阻止 url,但只能在应用程序重新启动后使用,这需要时间和浏览器窗口中的意外更改。
我希望能够在应用程序重新启动之前捕获直接浏览器导航。
如果这有帮助:我想完全禁止直接浏览器导航,所以我不需要允许某些 URL 并禁用其他 URL 的解决方案。
这是我的路由器定义:
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] },
// Callback MUST not be route guard protected.
{ 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 { }