angular2:可以停用防护 [英] angular2: CanDeactivate guard

查看:72
本文介绍了angular2:可以停用防护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CanDeactivate保护器,该保护器返回一个可观察到的值,并将其应用于加载在内部嵌套路由器出口中的组件.每当有人尝试导航到另一个URL时是否应调用此防护?我问这是因为我的情况没有发生.

I've created a CanDeactivate guard which returns an observable and it's applied to a component which is loaded in a inner nested router-outlet. Should this guard be called whenever one tries to navigate to another url? I'm asking this because this is not happening in my case.

就我而言,警卫只会被呼叫第一个不同" URL.让我尝试用一​​个例子来解释它.假设我总是返回false,并且试图从同一个组件导航到不同的url:

In my case, the guard will only get called for the first "different" URL. Let me try to explain it with an example. Assume I'm always returning false and I'm trying to navigate to different urls from the same component:

/A --> guard called
/B --> guard called
/B --> no navigation and no guard called
/A --> guard called
/A -->guard not called and no navigation

这是预期的行为吗?

编辑嗯,好像是这样.刚刚构建了一个包含3个组件的小样本,并且只有在用户第一次尝试导航到特定网址时才会调用防护罩.这真的很奇怪...

edit Well, it seems like it is. Have just built a small sample with 3 components and the guard will only be called for the 1st time the user tries to navigate to a specific url...this is really weird...

无论如何,这是我正在使用的代码:

Anyways, here's the code I'm using:

// 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>

//app.component.html <h1> <a routerLink="/main">Main</a> <a routerLink="/other">Other</a> <a routerLink="/other3">Other3</a> </h1>

一切都是通过angular-cli生成的(例如:n g组分XXX).是的,CanDeactivate防护始终将返回false,因此您将无法卸载主要组件.因此,当我第一次单击其他按钮时,警卫会被呼叫.如果再次单击其他,则不会召集警卫.但是,如果我单击other3,则警卫将被呼叫.在我单击其他链接(例如:其他)之前,单击other3并不会做任何事情.

Everything was generated through angular-cli (ex.: n g component XXX). Yes, the CanDeactivate guard will always return false so you won't be able to unload the main component. So, the first time I click other, the guard gets called. If click again on other, no guard gets called. However, if I click over other3, then the guard gets called. Clicking over other3 won't really do anything until I've clicked on other link (ex.: other)...

这是预期的行为吗?我必须说,我希望我的警卫每次点击另一个链接时都会受到攻击...

Is this the expected behavior? I must say that I expected my guard to get hit everytime I hit another link...

谢谢.

路易斯

推荐答案

该死,错误...自我说明:下次,请首先检查问题委员会

Damn, bug...Note to self: next time, check the issues board first

https://github.com/angular/angular/issues/12851 #event-880719778

这篇关于angular2:可以停用防护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆