属性是私有的,只能在 Angular 库中的类错误中访问 [英] Property is private and only accessible within class error in Angular Library
问题描述
我正在尝试在可重用库中转换 Angular 6 应用程序;这是我所做的:
I am trying to convert an Angular 6 application in a reusable library; here is what I did:
- 创建了一个新的 Angular 工作区
- 在该工作区中创建了一个新的 Angular 库项目
- 从库内的旧应用程序复制工件并调整导入语句
当我尝试使用ng build --project=.."编译项目时,我收到了很多这样的错误:属性 X 是私有的,只能在类 Y 内访问"
When I try to compile the project with "ng build --project=.." I get a lot of these errors: "Property X is private and only accessible within class Y"
我理解错误,我会纠正它们,但我的问题是:如果我尝试使用 aot 构建旧应用程序,我不会收到此错误..为什么?
I understand the errors and I will correct them but my question is: If I try to build the old application with aot I don't get this error..why?
谢谢!
推荐答案
在 AOT 编译中,Angular 会静态分析您的代码.使用错误的访问修饰符是导致 AOT 编译失败的一个标准,而在没有 AOT 的构建中编译成功.
In the AOT compile, Angular statically analyzes your code. Using wrong access modifiers is a criteria that lets the AOT compile fail, while the compile succeeds in a build without AOT.
这是因为在及时"模式下(通常是您的 ng 开始),没有使用 Typescript - 正在生成 ES5 代码,因此没有私有"字段这样的概念,我们无法强制执行此操作.
This is the way it is because in the "just in time" mode (which usually is your ng start), Typescript is not being used - ES5 code is being generated, so there's no such concept as a "private" field, and we have no way of enforcing this.
从模板访问的东西必须是公开的,因为它们是在类实例之外访问的.
Things that are accessed from a template must be public, since they're being accessed outside of the class instance.
这篇关于属性是私有的,只能在 Angular 库中的类错误中访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!