- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 CanDeactivate 守卫,它返回一个 observable,并将其应用于一个加载到内部嵌套路由器导出中的组件。每当有人试图导航到另一个 url 时,是否应该调用这个守卫?我问这个是因为我的情况没有发生。
在我的例子中,守卫只会在第一个“不同的”URL 时被调用。让我试着用一个例子来解释它。假设我总是返回 false 并且我正在尝试从同一组件导航到不同的 url:
/A --> guard called
/B --> guard called
/B --> no navigation and no guard called
/A --> guard called
/A -->guard not called and no navigation
这是预期的行为吗?
edit 嗯,好像是这样。刚刚构建了一个包含 3 个组件的小示例,并且只会在用户第一次尝试导航到特定 url 时调用守卫...这真的很奇怪...
无论如何,这是我正在使用的代码:
// app.routing
import {NgModule} from "@angular/core";
import {Routes, RouterModule, Route, CanDeactivate, ActivatedRouteSnapshot,
RouterStateSnapshot} from "@angular/router";
import { MainComponent } from "./main/main.component";
import { OtherComponent } from "./other/other.component";
import { Other3Component } from "./other3/other3.component";
import {Observable} from "rxjs/observable";
const fallback: Route = {
path: "**",
redirectTo: "/main",
pathMatch: "full"
};
export class Test implements CanDeactivate<MainComponent>{
canDeactivate(component: MainComponent, route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean{
console.log("in");
return false;
}
}
export const rotas: Routes = [
{
path: "main",
component: MainComponent,
canDeactivate: [Test]
},
{
path: "other",
component: OtherComponent
},
{
path: "other3",
component: Other3Component
},
fallback
];
@NgModule({
imports: [RouterModule.forRoot(rotas)],
exports: [RouterModule]
})
export class AppRoutingModule{}
//app.component.html <h1>
<a routerLink="/main">Main</a>
<a routerLink="/other">Other</a>
<a routerLink="/other3">Other3</a>
</h1>
一切都是通过 angular-cli 生成的(例如:n g component XXX)。是的,CanDeactivate 守卫将始终返回 false,因此您将无法卸载主要组件。所以,我第一次点击其他时, guard 被召唤。如果再次单击其他,则不会调用守卫。但是,如果我点击 other3,就会调用守卫。在我点击其他链接(例如:其他)之前,点击 other3 不会真正做任何事情......
这是预期的行为吗?我必须说,每次我点击另一个链接时,我都希望我的守卫被击中......
谢谢。
路易斯
最佳答案
我已经找到了这个解决方案,而不是为每个组件创建一个 candeactivate 守卫,你将创建一个守卫服务并为你想要添加这个选项的每个组件添加一个 candeactivate 方法,所以首先你必须添加这个服务文件“停用-guard.service.ts”:
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs/Observable';
export interface CanComponentDeactivate {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
@Injectable()
export class DeactivateGuardService implements CanDeactivate<CanComponentDeactivate>{
canDeactivate(component: CanComponentDeactivate) {
return component.canDeactivate ? component.canDeactivate() : true;
}
}
然后你必须在应用模块中提供:
providers: [
DeactivateGuardService
]
现在在你要保护的组件中,添加函数:
export class ExampleComponent {
loading: boolean = false;
//some behaviour that change the loading value
canDeactivate() {
console.log('i am navigating away');
if (this.loading) {
console.log('no, you wont navigate anywhere');
return false;
}
console.log('you are going away, goodby');
return true;
}
}
可以看到变量加载是组件本地的。最后一步是将指令添加到路由模块中的组件:
{
path: 'example',
canDeactivate: [DeactivateGuardService],
component: ExampleComponent
}
就是这样,我希望这对您有所帮助,祝您好运。
关于 Angular 2 : CanDeactivate guard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148020/
我正在尝试在 Angular 7 中设置一个简单的 canDeactivate 守卫,我尝试了一些在线教程中的代码,但它们都产生了相同的错误: "Type 'CanDeactivate' is not
我正在开发一个 angular 应用程序,如果用户在编辑时离开页面时未保存更改,我需要显示一条警报消息。 我已经在路由中创建并注册了一个 candeactivate 保护,它在父组件中工作正常。但不能
我正在尝试在路由中使用 CanDeactivate 函数,但在另一条路由上调用时从未调用 canDeactivate 函数。 CanDeactivateGuard : import { Injecta
我已经实现了一个 CanDeactivate 保护来避免用户在上传过程中离开页面并且它有效。问题是我的消息始终是默认消息(由于浏览器语言而使用荷兰语),即使我自己设置消息,仍然显示相同的默认 conf
我正在尝试在我的组件中添加 CanDeactivate 功能。我有一个其中有一个输入字段和按钮的表单。我希望如果用户在输入字段中输入某些内容并移动到下一个屏幕而不提交,它将显示一个对话框。如果用户从对
每当 canDeactivate() 返回 false 时,我都会遇到一些问题,它会更改窗口历史记录,因为它变为 true 并且如果我按下后退按钮。我正在导航到其他一些 URL 或应用程序本身之外。
这是我的 canDeactivate 守卫,它可以工作。但是我不想在使用提交按钮时调用守卫。只有当我通过任何其他方式导航时。怎么办? import { Injectable } from '@angu
我创建了一个 CanDeactivate 守卫,它返回一个 observable,并将其应用于一个加载到内部嵌套路由器导出中的组件。每当有人试图导航到另一个 url 时,是否应该调用这个守卫?我问这个
在我当前的注销调用中,我有这样的事情: getLogoutConfirmation(){ // get confirmation in modal and perform logout
我一直在尝试编写一个 canDeactivate 守卫来检查来自可观察流的保存状态并相应地工作。 我的服务是这样的 import { Injectable } from '@angular/core'
我有一个确认/取消模式对话框,当用户离开路线时弹出。我通过使用带有 canDeactivate 方法的守卫来做到这一点。但是,我希望 canDeactivate 等到它从模态返回任何内容之前得到响应。
在我的 Ionic 5 应用程序中,我有以下导航路径。 PageHome -> PageA -> PageB 我已经为 PageA 实现了 CanDeactivate 保护。 export clas
我为 CanDeactivate 功能添加了以下路由器保护,以验证未保存的更改,这对我来说工作正常 导出接口(interface) CanComponentDeactivate { canDeacti
我正在尝试使用 canDeactivate 路由器防护。我传入的组件 (ClaimsViewComponent) 在代码第一次运行时为空。但在随后的运行中,代码按预期运行。 我们试图弄清楚为什么组件在
是否可以在 Angular 2 RC5 路由器的 CanDeactivate 防护中获得预期的路线?我看到有关 CanActivate ( CanActivate ) 的类似问题的答案,但这在 Can
我想用 canDeactivate 中的 Alertify 确认替换经典确认。我尝试实现以下代码,但单击“确定”时它不会返回 True。有人可以就此提出建议吗? import { Injectable
这是我的付款 component.ts ,我在其中使用了 Candeactivate 这样,每当用户尝试从该组件移动到其他组件时,我都会收到提示。它工作正常,现在的问题是,当用户完成付款并且当我尝试路
在我的 Angular 4 应用程序中,我有一些带有表单的组件,如下所示: export class MyComponent implements OnInit, FormComponent {
如果用户从带有脏表单的页面导航,我成功地使用 canDeactivate 提供警告消息: 我使用的代码在这里: is_submit = false; canDeactivate() {
我正在实现 CanDeactivate我的主要组件之一中的功能。为了测试它,我让它总是返回 false所以路线不能改变。 这是CanDeactivate调用 component.canDeactiva
我是一名优秀的程序员,十分优秀!