简体   繁体   English

Angular 从 RouterStateSnapshot 检查组件实例

[英]Angular check component Instance from RouterStateSnapshot

I'm trying to implement a canDeactivate guard.我正在尝试实现一个canDeactivate守卫。 For that purpose I need to know the component Instance of the route I want to navigate to.为此,我需要知道要导航到的路由的组件实例。

canDeactivate(
    component: ComponentCanDeactivate,
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot,
    nextState: RouterStateSnapshot
): Observable<boolean> {
    return new Observable((observer: Observer<boolean>) => {
        console.log('componet', component);
        console.log('state', state);
        console.log('nextstate', nextState);
        this.routerService.doSomething(nextState, route);
        observer.next(true);
    });
}

I wonder wheter its possible to get the component from nextState?我想知道是否可以从 nextState 获取组件? I tried something like this:我试过这样的事情:

nextState.root.component nextState.root.component

but it returns the AppComponent to me, since that's not the name of the component I am navigating to.但它会将AppComponent返回给我,因为这不是我要导航到的组件的名称。

How do I do this?我该怎么做呢? Or should I get an instance in other way?或者我应该以其他方式获得一个实例?

Edit:编辑:
i can see the Component Instance(NextComponent) in the developer terminal of my browser inside of RouterStateSnapshot -->我可以在 RouterStateSnapshot 内的浏览器的开发人员终端中看到组件实例(NextComponent)->

_root: TreeNode  
url: "/Nextcomponent/15708/childcomponent"  
_root: TreeNode  
children: Array(1)  
0: TreeNode  
children: Array(1)  
0: TreeNode  
children: Array(1)  
0: TreeNode  
children: Array(1)  
0: TreeNode  
children: Array(1)  
0: TreeNode  
children: []  
value: ActivatedRouteSnapshot  
component: class NextComponent
                 ^^^^^^^^^^^^^

But not able to get it inside my code.但无法在我的代码中获取它。

I see 2 things:我看到两件事:

  1. you should never use new Observable() , and and I don't see why you put your code inside an observable (you may just remove the Observable).你永远不应该使用new Observable() ,而且我不明白为什么你把你的代码放在一个 observable 中(你可能只是删除 Observable )。
  2. I would recommand implementing a method canDeactivateMyFeatureGuard in your component.我建议在您的组件中实现一个方法canDeactivateMyFeatureGuard You can make your component implement a generic AppCanDeactivateMyFeatureGuard (where your guard is named MyFeatureGuard ), which defines canDeactivateMyFeatureGuard .你可以让你的组件实现一个通用的AppCanDeactivateMyFeatureGuard (你的守卫被命名为MyFeatureGuard ),它定义了canDeactivateMyFeatureGuard
    Then you call this method in the guard.然后你在警卫中调用这个方法。

It would look like this:它看起来像这样:

export class MyComponent implements AppCanDeactivateMyFeatureGuard {
  // ...
  canDeactivateMyFeatureGuard(): boolean {
    return this.isLocked; // or whatever condition owned by the component, or service call...
  }
}

and the guard:和警卫:

export class MyFeatureGuard implements CanDeactivate {
  // ...
  canDeactivate(
                component: ComponentCanDeactivate,
                route: ActivatedRouteSnapshot,
                state: RouterStateSnapshot,
                nextState: RouterStateSnapshot
              ): boolean {
    // logic is now in each component, where it can handle the way you wish.
    return component.canDeactivateMyFeatureGuard(); 
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM