【发布时间】:2021-11-22 17:41:38
【问题描述】:
在一个项目中,我想将我的库切换到 ivy 部分编译模式(角度 12)。但是现在出现了一些令人讨厌的循环依赖错误:
错误 来自示例
✖ 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
很清楚为什么会有一个循环,但我无法找到一个解决方案来让它工作。组件 A 内部有组件 B,而 B 内部有组件 A。它创建了类似于用户可定义 UI 的东西。一个组件可以包含另一组动态添加的组件,如果你喜欢它是递归的。
编辑:请考虑文档中的此信息:https://angular.io/errors/NG3003#libraries“正常”项目可以使用此循环-dep,但库不能!在 stackblitz 中所有示例都有效,甚至是我的,因为在 stackblitz 上它不是一个库。
原始项目相当大,有一些这样的循环。所以这是一个简单的例子:
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 {
}
}
【问题讨论】:
-
检查此解决方案,看看它是否适合您。
stackoverflow.com/questions/70047200/…
标签: angular circular-dependency angular-library angular12 angular-ivy