为什么在 Angular 9 中使用 primeng p-radiobutton 会导致本机元素错误?

Why does usage of primeng p-radiobutton cause native Element error in Angular 9?

我有一个项目,最近从 Angular 8 迁移到了 Angular 9 。该项目使用 primeng version : 4.3.0 。现在,p-radiobutton 标记在整个项目中被广泛用于显示单选按钮,但它无法正常工作。 p-radiobutton 标签给出了运行时错误:Unable to get property 'nativeElement' of undefined or null reference 由于这个原因,UI 被扭曲了。整个错误堆栈跟踪如下所示:

TypeError: Unable to get property 'nativeElement' of undefined or null reference
   at RadioButton.prototype.writeValue (http://localhost:4200/vendor.js:115296:9)
   at setUpControl (http://localhost:4200/vendor.js:79928:5)
   at NgModel.prototype._setUpStandalone (http://localhost:4200/vendor.js:82718:9)
   at NgModel.prototype._setUpControl (http://localhost:4200/vendor.js:82706:32)
   at NgModel.prototype.ngOnChanges (http://localhost:4200/vendor.js:82630:13)
   at checkAndUpdateDirectiveInline (http://localhost:4200/vendor.js:67503:9)
   at checkAndUpdateNodeInline (http://localhost:4200/vendor.js:76327:13)
   at checkAndUpdateNode (http://localhost:4200/vendor.js:76289:9)
   at prodCheckAndUpdateNode (http://localhost:4200/vendor.js:76833:5)
   at Anonymous function (Function code:194:5)

html 中引发错误的代码行是:

<td class="alignHere">
    <p-radioButton name="myStuff" value="CAT" [(ngModel)]="StuffChosen"></p-radioButton>
</td> 

当我注释掉上面的代码时,错误不再出现,但我需要使用单选按钮功能。

有没有人知道这个错误是什么以及这个问题的潜在解决方案是什么?非常感谢任何帮助。

primeng 将版本与 angular 版本对齐,这意味着 primeng 10 用于 angular 10 ,primeng 11 用于 angular 11 这适用于旧版本。

你得到一个错误,因为在 primeng 4 中一些 angular api 已经改变或删除。

为了解决您的问题,只需选择您的 angular 应用版本

的 primeng 版本基础