Angular 6:奇怪的“属性xxx受到保护,只能在类及其子类中访问

问题描述 投票:1回答:1

我有一个库项目,我正在迁移到Angular 6和Angular CLI 6。

有一个名为NativeFormElementChild的抽象类,它有一些私有属性:_id_required_ariaDescribedBy_ariaRequired

然后我有一个指令,InputDirective,它被应用于<input>作为属性(添加了一些额外的功能)。 InputDirectiveNativeFormElementChild延伸。

现在,当我尝试在Angular 6中构建它时,我得到的错误很多。我到处使用我的InputDirective,我得到四个错误:

  • 属性'_id'受到保护,只能在类'NativeFormElementChild'及其子类中访问。
  • 属性“_required”受到保护,只能在“NativeFormElementChild”类及其子类中访问。
  • 属性'_ariaDescribedBy'受保护,只能在类'NativeFormElementChild'及其子类中访问。
  • 属性'_ariaRequired'受到保护,只能在类'NativeFormElementChild'及其子类中访问。

我不是试图从NativeFormElementChild内部的任何地方访问这些属性。所以我不知道这些错误来自哪里。使用Angular 5,我没有得到任何这些错误。

javascript angular typescript angular6
1个回答
0
投票

在组件html中,将*ngIf="true"放在最外面的html标记中。这似乎激活了Angular 6对受保护属性的理解。任何*ng标签(模板绑定)都可以。每个组件html都需要一个。

<div *ngIf="true">
...Everything in here will now properly have access to protected properties
</div>

或者,如果你没有外部元素,你可以使用ng-container ...

<ng-container *ngIf="true">
...
</ng-container>
© www.soinside.com 2019 - 2024. All rights reserved.