I have 3 files with a circular dependency, but understand that 2 of the imports are just ARRAYS, not components or injectors, but 1 of them is a Injector:
I highlighted the lines with the imports and its uses on the code bellow:
components.ts (this file is just a compilation of all my components, routes and routing to import inside the app.module, app.routing and menu.module:
import { ProvidersRoutes } from 'providers.routes'; // <<<<<<<<<<<<<<<<<<<
import { ServicesRoutes} from 'services.routes';
import { OffersRoutes } from 'offers.routes';
export const ROUTES= [ ...ProvidersRoutes, ...ServicesRoutes, ...OffersRoutes ]; // <<<<<<<<<<<<<<<<<<<
...
providers.routes.ts (this is a list of routes from the Providers component to be used in the menu.module):
import { AuthGuard } from '../../login/auth.guard'; // <<<<<<<<<<<<<<<<<<<
export const ProvidersRoutes: Routes = [
{
path: 'providers',
loadChildren: 'app/components/providers/providers.module#ProvidersModule',
canActivate: [AuthGuard], // <<<<<<<<<<<<<<<<<<<
data: {
state: 'providers',
groupRoles: ['ADMIN'],
}
},
];
...
auth.guard.ts (auto explained, it loads the ROUTES variable just to know what is the initial route):
...
import { ROUTES } from '../components/components'; // <<<<<<<<<<<<<<<<<<<
@Injectable()
export class AuthGuard implements CanActivate {
routes: Routes = [ // <<<<<<<<<<<<<<<<<<<
...ROUTES
];
initialRoute:string;
constructor(private router: Router, private loginService:LoginService, public snackBar: MatSnackBar) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean {
// set the initialRoute
if(this.loginService.isLogged()){
this.initialRoute = this.routes.filter((item)=>{ // <<<<<<<<<<<<<<<<<<<
return typeof(item.data.initial) != "undefined";
})[0].path;
} else {
this.initialRoute = '/login';
}
}
...
So, this circular dependency doesnt cause any bug, but keep appearing in my browser's and terminal consoles as a Warning as you can see bellow:
Circular dependency detected: src\app\components\components.ts -> src\app\components\providers\providers.routes.ts -> src\app\login\auth.guard.ts -> src\app\components\components.ts
And I have 2 questions:
1 - Should I fix it? Since doesn't cause any bug and 2 of the dependencies are not related to functions but just to arrays
2 - If I should fix it, what would be the best way?