I am posting new question based on the following conversation:
Original question
Here is the code provided in the original question:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class DetachedComponentTrackingService { }
The strategy would look similar to:
import { inject } from '@angular/core';
import {
ActivatedRouteSnapshot,
RouteReuseStrategy
} from '@angular/router';
import {
DetachedComponentTrackingService
} from './.../detached-component-tracking.service';
export class DetachedAwareRouteReuseStrategy
extends RouteReuseStrategy {
private readonly functionInjectedService =
inject(DetachedComponentTrackingService);
constructor(
private readonly constructorInjectedService: DetachedComponentTrackingService
) {
// RouteReuseStrategy
super();
}
override shouldDetach(route: ActivatedRouteSnapshot): boolean {
// this.functionInjectedService...
// this.constructorInjectedService...
throw new Error('Method not implemented.');
}
// Other overrides...
}
Then just provide the strategy like any other provider token:
import { AppComponent } from './.../app.component';
import { RouteReuseStrategy } from '@angular/router';
import {
DetachedComponentTrackingService
} from './.../detached-component-tracking.service';
import {
DetachedAwareRouteReuseStrategy
} from './.../detached-aware-route-reuse-strategy';
bootstrapApplication(AppComponent, {
providers: [
{
provide: RouteReuseStrategy,
useClass: DetachedAwareRouteReuseStrategy,
},
provideRouter([
// Routes...
]),
],
});
I have followed it and compiled it ok, but when I run it I am getting:
main.ts:18 Could not load file 'assets/config/config.dev.json': {"name":"NullInjectorError","ngTempTokenPath":["InjectionToken HTTP_INTERCEPTORS",
"[object Object]","Router","RouteReuseStrategy","DetachedComponentTrackingService","DetachedComponentTrackingService"]}
If I try injecting a service that is located in the same root folder where awa-route-reuse-strategy.ts is then it works fine. Any idea how to make it work with a service located anywhere in the app?
Thanks