- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何说服 Angular 4 路由器在我的应用中禁用直接浏览器导航?
我查看了路由器 Hook 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 { }
最佳答案
以下解决方案不是基于 Angular 的解决方案。正如 Maximus 在他的回答中解释的那样,这是一个浏览器问题,因此解决方案必须基于浏览器。
此解决方案源自本网站 ( see here ) 上的一个答案,我使用以下三种方法编写了一个小的 (typescript) 服务:
private preventNavigation(): void {
const location = window.document.location;
const originalHashValue = location.hash;
window.setTimeout(() => {
location.hash = 'preventNavigation' + (9999 * Math.random());
location.hash = originalHashValue;
}, 0);
}
public disableBrowserNavigation(): void {
window.addEventListener('beforeunload', this.preventNavigation, false);
window.addEventListener('unload', this.preventNavigation, false);
}
public enableBrowserNavigation(): void {
window.removeEventListener('beforeunload', this.preventNavigation);
window.removeEventListener('unload', this.preventNavigation);
}
这很好用,但我仍然对其他解决方案持开放态度。
关于 Angular 4 路由器 : prevent direct browser navigation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427632/
在JSON输出中,“步骤”中有一个字段“maneuver”。在此“向左转”,“向右转”,“向左转轻微”等。示例为here 在哪里可以找到“操纵”字段的定义以及可能的值列表?没有相关描述here 提前致
默认情况下,我们如何从Google Direction API建议的替代 route 获得从A点到B点的最短距离路线?默认情况下,它会根据当前交通状况为我们提供最短持续时间的路线。我已经注意到,如果您
我想知道“precompile(r) directive”和“preprocessor directive”是一回事吗?我对前者不熟悉,但只是听说过,并通过这个Google在互联网上找到了一些关于它的
对于我的项目,我目前正在开发自定义表单/输入指令。 例如,我有以下指令: angular.module('myApp').directive("textField", function() {
我用谷歌搜索了一下,但找不到任何详细说明如何制作动态包装内容的 Angular 指令的内容(例如 http://demos.telerik.com/kendo-ui/panelbar/angular
我正在尝试在另一个指令中使用一个指令。具体来说,我有一个模态指令,我想传递一个表单指令,并将充当模态的主体。 我的模态指令: angular.module('Storyboard').dir
我构建了一个模块化形式的小型演示,其中包含单独的输入指令。它还可以预览绑定(bind)到相同 Controller 和范围的表单值。 导致问题的输入指令是嵌入到表单内的输入:
HTML 指令 .directive('authorname', function() { return { restrict: 'E', scope: {
我有可以编译的 Angular 指令 至和 至Hello World! 我怎样才能把greeting在我的 HTML 中标记并将其编译为 print-greeting然后最后显示Hello World
标题中引用的脚注是什么意思?这是 6.10.3p11 的脚注 If there are sequences of preprocessing tokens within the list of arg
因此,电话号码始终是 ltr(从左到右)。 在多语言网站上工作,我需要在方向为 rtl 的文本段落中插入一个电话号码(带有“+”前缀和由“-”分隔的数字)(当然是针对相关语言) 所以我有这样的东西:
我有一个标题元素,我想显示 flex 列,这样我就可以将 .container div 垂直居中。这工作正常。然后我需要 .container 中的元素在 1200px 之间以均匀的间距连续 flex
如何将整个ng-repeat对象传递给指令(或如何将指令的作用域设置为ng-repeat项)? 我是新来的有角度的人,很难解决这个问题。 我有一个 Controller ,可以很好地呈现以下内容:
我需要将“...”放在文本前面,并在填充 div 时仅显示文本的最后一部分。 正常时不执行任何操作 C:\fakepath\996571_1398802860346752_209456547
我需要将“...”放在文本的前面,并且只显示它的最后一部分,当它填充 div 时。 正常的时候什么也不做 C:\fakepath\996571_1398802860346752_209456
我需要一个“粘性”指令,当它位于页面顶部时向元素添加一个 css 类,并且还指示其状态的变化。出于这个原因,我定义了一个范围,如 { onStickyChange: '&' }。现在我想在 angul
我对 ngSwitch 指令有点困惑——它是“属性指令”还是“结构指令”。 属性指令用“方括号”编写,如 [ngStyle]、[ngClass] 等(我们将其写为 [ngSwitch],将其称为“属性
Wi-Fi direct 的 Wiki 规范声称“只有一个 Wi-Fi 设备需要兼容 Wi-Fi Direct 才能建立点对点连接,在彼此之间直接传输数据,大大减少了设置”。但是从 android A
我有一个响应式模板,我正尝试将其与我的 Angularjs 应用程序一起使用。这也是我的第一个 Angular 应用程序,所以我知道我在未来有很多错误和重构。 我已经阅读了足够多的关于 Angular
首先,我这样做的方式可能不正确。但我会解释这个问题: 1) 我正在创建名为 的指令 2) 当点击第一个指令中的按钮时,我试图在运行时动态插入第二个指令 如下: var app = angu
我是一名优秀的程序员,十分优秀!