gpt4 book ai didi

angular - AoT NGC/Angular2 : Property is protected and only accessible within class Error

转载 作者:太空狗 更新时间:2023-10-29 17:18:02 27 4
gpt4 key购买 nike

在我正在使用的 Angular 2/Ionic 2 (final/rc0) 项目中的一个组件中:

protected contentTarget: ViewContainerRef;

ngOnInit() {
this.contentTarget.createComponent(componentFactory);
}

AoT 编译器说:

Error at ....: Property 'contentTarget' is protected and only accessible within class 'IncludeTemplateComponent' and its subclasses.

变量(属性)未在整个项目的任何其他地方使用。

所以...任何人都可以阐明这一点,是 createComponent 工厂将 contentTarget 变量传递给它的子变量,还是为什么编译器不喜欢在这里 protected ? protected 变量现在在所有 Angular2 中都被“禁止”了吗?

最佳答案

来自 here

For a given component all its members (methods, properties) accessed by its template must be public in the ahead-of-time compilation scenario. This is due to the fact that a template is turned into a TS class. A generated class and a component are 2 separate classes now and you can't access private members cross-class.

我的看法是 contentTarget 由模板引擎使用,因此必须公开 AOT 才能工作。

关于angular - AoT NGC/Angular2 : Property is protected and only accessible within class Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020645/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com