0

I'm using Angular standalone (bootstrapApplication etc.).

I'm following the docs here Providing services to a subset of routes to lazy load a service which works.

export const ROUTES: Route[] = [
  {
    path: 'admin',
    providers: [
      AdminService,
      {provide: ADMIN_API_KEY, useValue: '12345'},
    ],
    children: [
      {path: 'users', component: AdminUsersComponent},
      {path: 'teams', component: AdminTeamsComponent},
    ],
  },
  
  // ... other application routes that don't
  //     have access to ADMIN_API_KEY or AdminService.
];

My issue is that when I navigate outside the admin/ route the service doesn't self destroy. I'm logging out ngOnDestroy and constructor calls to confirm this.

  • I've left the service with no providedIn i.e. @Injectable().
  • It's not included it in the bootstrap providers array, only the route providers.
  • inside the admin/ routes each component has a provided store service component that injects the AdminService via service = inject(AdminService). Each store service does get destroyed on navigation.

Should the service self destroy automatically?

This answer to another question suggests not https://stackoverflow.com/a/75835854/4711754 but I wanted to check before coming up with a way of force destroying the AdminService.

Andrew Allen
  • 6,512
  • 5
  • 30
  • 73

0 Answers0