简体   繁体   English

Angular 组件:扩展 class 的实例似乎被覆盖

[英]Angular components: instance of extended class seems to get overwritten

In my angular 8 project I have a class (just a class, not implemented as a component) that provides some default configuration and methods.在我的 angular 8 项目中,我有一个 class(只是一个 class,未作为组件实现),它提供了一些默认配置和方法。

This class gets extended by multiple components.这个 class 被多个组件扩展。 If multiple components on the same view (ie that exist at the same time) extend that class their instances seem to overwrite each other.如果同一视图上的多个组件(即同时存在)扩展 class,它们的实例似乎会相互覆盖。 If I overwrite an attribute or a method in 1 of the components and another one gets instantiated afterwards, the overwrites vanish.如果我覆盖其中一个组件中的属性或方法,然后另一个组件被实例化,则覆盖消失。

I have no idea why that would happen.我不知道为什么会这样。 The constructor of the extended class gets called multiple times, so in theory, every extending class should create its own instance.扩展 class 的构造函数被多次调用,因此理论上,每个扩展 class 都应该创建自己的实例。

Update更新

I recreated the problem in this StackBlitz demo: https://stackblitz.com/edit/angular-hw1cv9我在这个 StackBlitz 演示中重现了这个问题: https://stackblitz.com/edit/angular-hw1cv9

Both ListComponent and PopupComponent extend DataGridComponent. ListComponent 和 PopupComponent 都扩展了 DataGridComponent。 In ListComponent I enable the export functionality in the template and that always works.在 ListComponent 中,我在模板中启用了导出功能并且始终有效。 I also overwrite the onExporting event handler.我还覆盖了 onExporting 事件处理程序。 This overwrite works fine if you comment out the popup (or block it from being created with *ngIf="popupVisible") but it reverts to its original functionality with the popup as is.如果您注释掉弹出窗口(或阻止使用 *ngIf="popupVisible" 创建它),此覆盖工作正常,但它会按原样恢复到弹出窗口的原始功能。

(I can't figure out how to show devextreme icons in the demo, sorry. The export button is the left one above the table). (我不知道如何在演示中显示 devextreme 图标,抱歉。导出按钮在表格上方的左侧)。

Updated the demo with the solution使用解决方案更新了演示

You're setting the defaultOptions of the DataGrid Object itself.您正在设置 DataGrid Object 本身的 defaultOptions。 Any Class that inherits your Component or imports the DataGrid from somewhere else will the the config you've set within the DataGridComponents constructor.任何继承您的组件或从其他地方导入 DataGrid 的 Class 都将是您在 DataGridComponents 构造函数中设置的配置。

Are you sure you directly want to assign the defaultOptions to the DataGrid Object?您确定要直接将 defaultOptions 分配给 DataGrid Object 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM