angular - 固定 : Angular 9: Undecorated base classes that use Angular features

标签 angular typescript mixins angular9

我最近更新到了 Angular v9,并在更改日志中定义,不推荐使用未修饰的基类,该基类使用 Angular 功能或由指令或组件扩展。

所以我的应用程序中有很多 mixin 是这样的:

销毁:

export const Destroy = <T extends Constructor>(base: T = class {} as T) =>
  class extends base implements OnDestroy {
    destroy$ = new Subject<boolean>();

    ngOnDestroy(): void {
      this.destroy$.next(true);
      this.destroy$.complete();
    }
  };

卷轴:
export const Scroll = <T extends Constructor>(base: T = class {
} as T) =>
  class extends base {
    public scrollToFirstError(form: FormGroup, scrollSelector?: string) {
      form.markAllAsTouched();
      const target = jQuery('.ng-invalid:not("form")').first();
      const scrollContainer = jQuery(scrollSelector || 'html,body');
      const subHeaderHeight = scrollSelector ? 0 : Number.parseInt(getComputedStyle(document.documentElement)
        .getPropertyValue('--height').trim(), 10);
      scrollContainer.animate(
        { scrollTop: jQuery(target).offset().top - jQuery(scrollContainer).offset().top - subHeaderHeight - 50 }, 'slow');
      target.focus();
    }
  };

在我的组件中扩展它,例如,
export class ComponentA extends Destroy(Scroll)

并尝试访问这些 mixin 的属性会引发以下错误:
this.apiService.getData().pipe(takeUntil(this.destroy$)... // Property 'destroy$' does not exist on type 'ComponentA'.


this.scrollToFirstError(this.form, '.modal'); // Property 'scrollToFirstError' does not exist on type 'ComponentA'.

任何人都可以帮助我提出解决问题的建议吗?提前致谢!

******** 更新 *********

在我的组件中:
export const MixinedClasses: any = Destroy(Scroll());

  @Component({
  selector: 'app-loan-list',
  templateUrl: './loan-list.component.html',
  styleUrls: ['./loan-list.component.scss']
})
export class Component extends MixinedClasses {}

最佳答案

只需转换 DestroyScroll进入@component 装饰的组件,它应该可以正常工作。

添加:

// Destroy const becomes a decorated class
  @Component({
  selector: 'app-destroy'
})
export class Destroy implements OnDestroy{}

// Scroll const becomes a decorated class
  @Component({
  selector: 'app-scroll'
})
export class Scroll{}

这只是一个想法,您必须使其适合您的特定实现。

老实说,我不太了解您通过传递 base 扩展它们的方式。 in 作为参数,因此您可能需要使用标准类重新考虑整个方法。

关于angular - 固定 : Angular 9: Undecorated base classes that use Angular features,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257100/

相关文章:

javascript - 如何以 Angular 创建新数组

javascript - 如何从对象重命名字段

node.js - 更新文档时 XMLHttpRequest 错误,但应启用 CORS

typescript - 当测试/** 未包含在 tsconfig 中时,Vscode TS 语言功能不可用

postgresql - 如何在 TypeORM 中定义 varchar 和 enum?

angular - 如果我们在 Angular 4 中使用 *ngFor,如何在一个实例上触发一个函数

javascript - 如何使用 Angular 8 为某些回调(Leaflet 库)设置事件处理程序

c++ - 向 C++ Mixins 添加工厂层次结构

python - 混合 Detail View 和 Form Mixin django 的问题

javascript - Mixin 中的 Ember 访问变量