Angular - 组件蓝图

标签 angular

我有一个组件,它充当许多组件的蓝图。他们都共享相同的方法。我怎样才能从中制定蓝图?

我不想重复所有其他组件中的方法。如果我需要更改某些功能,我只想重写一个方法。

@Component({
    template: `
       <datagrid (onRefresh)="loadCollection($event)" (onRowSelect)="loadRecord($event)" (onCreateRecord)="createRecord()"></datagrid>  
    `,
    providers: [CollectionService]
})
export class ListComponent implements OnInit{

    loadCollection($event){
      ...
    }


    loadRecord($event){
      ...
    }

    createRecord(){
      ...
    }
}

最佳答案

尝试下面,

export class BaseComponent { 
  name: string ="";
  someCommonFunction(){
    return `Method called from Base Component from child ${this.name}`
  }
}

@Component({
  selector: 'child-1',
  template: `{{someCommonFunction()}}`
})
export class ChildComponent1 extends BaseComponent {
  constructor(){
    super();
    this.name = "ChildComponent1";
  }      
}

@Component({
  selector: 'child-2',
  template: `{{someCommonFunction()}}`
})
export class ChildComponent2 extends BaseComponent { 
   constructor(){
    super();
    this.name = "ChildComponent2";
  }
}

这是Plunker!!

希望这有帮助!!

关于Angular - 组件蓝图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651583/

相关文章:

路径中的 Angular 4 路由器 %2F 符号

angular - 使用社交登录将 Firebase 用户添加到用户数据库?

javascript - Angularfire2 AngularFirestoreCollection 不发送集合中每个对象的键

jquery - 使用 jQuery 插件时,Angular 应用程序无法编译(Foundation 6 Accordion )

json - SyntaxError : JSON. 解析:JSON 数据 Angular 2 的第 1 行第 1 列出现意外字符

java - 从客户端实现的 Angular 来看,我可以将 Java Callable 与 Angular Observable (RxJS) 进行比较吗

angular - 在 Angular 2 中使用 SystemJS 和 Karma 加载节点模块

angular - 无法将浏览器连接到 Karma 服务器(永久页面加载)

javascript - 使用 ng-select 包的 Jasmine 单元测试错误

javascript - Angular - 如果数组中存在值,则禁用选择选项