考虑以下情况:父组件在其模板中使用子组件,子组件对注入到其子组件的父组件具有引用。
parent.component.html:
<child></child>child.component.ts:
import { ParentComponent } from '../parent/parent.component.ts'
@Component({
selector: 'child'
...
})
export class ChildComponent {
constructor(private parent: ParentComponent) { }
}在用推荐的部分编译模式编译的库中使用此设置时,编译器将抛出NG3003错误。ideas文档提供了以下解决问题的思路:
- Using an interface in the child component to reference the parent component is not possible because the injector would not know what to inject.- I just don't want to do this. :)- Same issue as with an interface, the injector would not know what to inject.是否有另一种解决N3003错误的方法,而不将父组件和子组件移动到同一个文件中?
发布于 2021-07-09 20:15:57
在上述情况下的N3003错误可以通过使用InjectionToken来解决,如下所示:
发布于 2022-06-30 19:17:09
到目前为止,我只找到了一个解决方案:创建接口--比如说IParentComponent --枚举子组件中使用的所有属性和方法,并由父组件实现这个接口。然后在子组件中使用@Input() parentComponent: IParentComponent。不是很好的方法,如果有很多属性/方法.
https://stackoverflow.com/questions/68321910
复制相似问题