代码之家  ›  专栏  ›  技术社区  ›  Aivan Monceller

如何创建一个typescript装饰器,使装饰类扩展一个类

  •  0
  • Aivan Monceller  · 技术社区  · 6 年前

    我在创作装饰方面的知识非常有限,所以请容忍我。

    假设我们有一个基类,稍后将用于我们的decorator:

    export class BaseElement { 
         tag: string;
         // some other stuff the base class will do
    }
    

    我们有个装修师叫 Component

    @Component({
      tag: 'todo-list',
    })
    export class TodoList { }
    

    这是装饰班 TodoList 它应该延伸 BaseClass 当装修工 构建我们想要的类 并设置 tag

    这就是我想通过使用decorator实现的目标:

    export class TodoList extends BaseElement {
        constructor() {
          super()
          this.tag = ‘todo-list’;
        }
    }
    

    如果这是不可能的,有没有办法创建一个typescript插件使之成为可能?

    0 回复  |  直到 6 年前
    推荐文章