in a project I wanted to switch my libs to the ivy partial compilation mode (angular 12). But got now some nasty circular dependency errors:
Error from example
✖ Compiling with Angular sources in Ivy partial compilation mode.
An unhandled exception occurred: projects/circ/src/lib/tab-container/tab-container.component.ts:4:1 - error NG3003: One or more import cycles would need to be created to compile this component, which is not supported by the current compiler configuration.
4 @Component({
~~~~~~~~~~~~
5 selector: 'my-tab-container',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
42 }
~~~
43 }
~
projects/circ/src/lib/container/container.component.ts:4:1
4 @Component({
~~~~~~~~~~~~
5 selector: 'my-container',
~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
41 }
~~~
42 }
~
The component 'ContainerComponent' is used in the template but importing it would create a cycle: C:/projects/ng-test-projects/circular-dep-lib/projects/circ/src/lib/tab-container/tab-container.component.ts -> C:/projects/ng-test-projects/circular-dep-lib/projects/circ/src/lib/container/container.component.ts -> C:/projects/ng-test-projects/circular-dep-lib/projects/circ/src/lib/tab-container/tab-container.component.ts
It's clear why there are a cycles but I can't get a solution who to make it work. The component A has component B inside while B has a component A inside. It creates something like a user-definable UI. A component can contain a other set of dynamicly added components, recursive if you like it.
EDIT: Please consider this info from the doc: https://angular.io/errors/NG3003#libraries A "normal" project would work with this circular-dep but a library not! In stackblitz all examples works, even my, because on stackblitz it's not a lib.
The original project is quite large with some cylcles like this. So here is a bare bone example:
container.component.ts
import { Component, Input, OnInit } from '@angular/core';
import { UiItemLike } from '../ui-item-like';
@Component({
selector: 'my-container',
template: `
<h2>Container</h2>
<ng-container *ngFor="let item of uiItems">
<!-- Container -->
<!-- This recursion is working! The component itself is not "importing" from an other file -->
<ng-container *ngSwitchCase="item.type === 'Container'">
<my-container [uiItems]="item.uiItems"></my-container>
</ng-container>
<!-- Tab-Container -->
<ng-container *ngSwitchCase="item.type === 'TabContainer'">
<!-- Thats the circular dependency -->
<my-tab-container [uiItems]="item.uiItems"></my-tab-container>
</ng-container>
<!-- Button -->
<ng-container *ngSwitchCase="item.type === 'Button'">
<my-button></my-button>
</ng-container>
</ng-container>
`,
styles: [``]
})
export class ContainerComponent implements OnInit, UiItemLike {
@Input() uiItems: UiItemLike[];
readonly type: "Container";
constructor() {}
ngOnInit(): void {}
}
tab-container.component.ts
import { Component, Input, OnInit } from '@angular/core';
import { UiItemLike } from '../ui-item-like';
@Component({
selector: 'my-tab-container',
template: `
<h2>TabContainer</h2>
<div>
<h3>Fake tab 1</h3>
<!-- Can contain more items -->
<!-- Thats the circular dependency -->
<my-container [uiItems]="uiItems.uiItems"></my-container>
</div>
<div>
<h3>Fake tab 2</h3>
<!-- ... -->
</div>
`,
styles: [``]
})
export class TabContainerComponent implements OnInit, UiItemLike {
@Input() uiItems: UiItemLike[];
readonly type: "TabContainer";
constructor() { }
ngOnInit(): void {
}
}
https://stackoverflow.com/questions/70047200/ng3003-angular-12-circular-dependency-in-library-ivy-partial-compilationmode/70078485#70078485 – Shadowalker Nov 23 '21 at 09:44