首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修正:角9:使用角特性的未修饰基类

修正:角9:使用角特性的未修饰基类
EN

Stack Overflow用户
提问于 2020-02-17 06:39:14
回答 1查看 1.2K关注 0票数 4

我最近更新到了角v9,并且按照变更日志中的定义,不推荐使用角特性或由指令或组件扩展的未修饰基类。

因此,我的应用程序中有许多混频器,如下所示:

破坏:

代码语言:javascript
运行
复制
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();
    }
  };

卷轴:

代码语言:javascript
运行
复制
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();
    }
  };

在我的组件中扩展它,

代码语言:javascript
运行
复制
export class ComponentA extends Destroy(Scroll)

尝试访问这些混合器的属性会引发以下错误:

代码语言:javascript
运行
复制
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'.

有人能帮我解决这个问题吗?提前感谢!

* Update *

在我的部分:

代码语言:javascript
运行
复制
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 {}
EN

回答 1

Stack Overflow用户

发布于 2020-05-29 17:45:51

只需将DestroyScroll转换为@component修饰组件,它就会正常工作。

添加:

代码语言:javascript
运行
复制
// 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作为参数进行扩展的方式,因此您可能需要重新考虑使用标准类的整个方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60257100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档