【发布时间】:2019-05-06 07:46:55
【问题描述】:
如果模板中没有引用 ViewChild,是否可以将其标记为私有?
我已经能够做到这一点,然后使用“--prod”标志构建和服务,并且没有遇到任何错误。我目前正在使用 Angular 7。
@ViewChild(HelloComponent) private helloComponent;
这是我正在谈论的内容的堆栈闪电:
https://stackblitz.com/edit/angular-vxbpuk?file=src%2Fapp%2Fapp.component.ts
我认为我有使用 aot 的 stackblitz,但您可以在本地验证同样的事情。
编辑:对于之前没有提出这个问题,我深表歉意,但是 Angular 文档中的这个简介让我停下来:
https://angular.io/guide/aot-compiler#phase-2-code-generation
装饰的组件类成员必须是公共的。您不能将 @Input() 属性设为私有或内部。
但正如@GCSDC 在下面已经指出的那样,Angular 团队似乎在他们的示例中使用了私有成员:
https://angular.io/guide/component-interaction#parent-calls-an-viewchild
【问题讨论】:
-
这可能值得区分,
private可以,但 ES 类 #private 字段则不行——请参阅github.com/microsoft/TypeScript/issues/31670 了解他们为什么同时保留这两个字段的大讨论
标签: angular private angular7 viewchild angular-aot