0

I have console.log in constructor and ngOnInit() of Resolver but which are not logged.

resolve:{serverResolver:ServerResolverDynamicDataService}},

console.log("ServerResolverDynamicDataService constructor");

console.log('ServerResolverDynamicDataService resolve:'

const appRoutes : Routes =[
    {path:"",component:HomeComponent},
    {path:"servers",canActivateChild:[AuthGuardService],component:ServersComponent,
        children:[                
          {path:":id",component:ServerComponent,
           resolve:{serverResolver:ServerResolverDynamicDataService}},
          {path:":id/edit",component:EditServerComponent,canDeactivate:[CanDeativateGuardService]}]},

Resolver:

@Injectable()
export class ServerResolverDynamicDataService implements Resolve<ServerModel>{

    constructor(private serversService:ServersService){
        console.log("ServerResolverDynamicDataService constructor");
    }

    resolve(activatedRouteSnapshot: ActivatedRouteSnapshot, routerStateSnapshot: 
        RouterStateSnapshot): ServerModel | Observable<ServerModel> | Promise<ServerModel> {
            console.log('ServerResolverDynamicDataService resolve:'+activatedRouteSnapshot.params['id']);
        return this.serversService.getServer(+activatedRouteSnapshot.params['id']);
    }    
}

Update1: app.module.ts has entry of this service in providers

  providers: [ServersService,AuthGuardService,AuthService,CanDeativateGuardService,ServerResolverDynamicDataService],

Whenever URL(http://localhost:4200/servers/1?allowToEdit=0&allowTest=2#loadPage) is getting hit, no logs are coming from resolver but logs are there in code and the application is properly refreshing if I edit any other part of the application log. So app changes are reflecting the only problem is resolver is not called.

Update2 As per Angular 2+ route resolvers not being called if I remove parent canActivateChild service which is working.B ut I don't know what is wrong.Please help me to understand.

sunleo
  • 10,589
  • 35
  • 116
  • 196

1 Answers1

1

A Resolver is a service and OnInit isn't executed in a service. In fact, besides OnDestroy, there's no other lifecycle hook in a service.

Anyway, I'm assuming your resolver is provided somewhere. If it's not, you should use an argument in its decorator: @Injectable({providedIn: 'root'}).

julianobrasil
  • 8,954
  • 2
  • 33
  • 55
  • Thanks for reply,Service is registered in app.module as providers: [ServersService,AuthGuardService,AuthService,CanDeativateGuardService,ServerResolverDynamicDataService], – sunleo Apr 20 '20 at 03:30