Search code examples
angulartypescriptangular-routingangular-components

Angular check service instance


I'm trying to implement a canDeactivate guard. For that purpose I need to check properties of the service instance on the component. However, since this service may be injected using different aliases I try to check all properties to be an instance of a service.

canDeactivate(
    component: Component,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState ? : RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {

    let tabsService;

    const componentProps = Object.entries(component);

    console.log(componentProps);

    for (let i = 0; i < componentProps.length; i++) {
        if (componentProps[1] instanceof TabsService) {
            console.log(true);
            tabsService = componentProps[1];
            break;
        }

        console.log(componentProps[1] instanceof TabsService);
    }

    ...
}

However, this results in all falses.enter image description here

How do I do this? Or should I get an instance in other way? I know that it's bad to call one thing in different aliases, but this is inevitable with tons of DIs and team development (IMHO).


Solution

  • It should be componentProps[i][1] everywhere instead of componentProps[1], shouldn't it ?