【问题标题】:It's good practice to use class inheritance for components in Angular? What about nested inheritance? [closed]对 Angular 中的组件使用类继承是一种好习惯吗?嵌套继承呢? [关闭]
【发布时间】:2021-08-20 11:49:57
【问题描述】:

如果我有这样的课程:

common.ts(在 mixins 文件夹下):

export class Common { }

基础组件.ts:

@Mixin([Validation])
export abstract class BaseComponent extends Common implements Validation {...}

base-list-type-component.ts:

@Injectable()
class BaseListComponent<T, U> extends BaseComponent implements OnInit, OnDestroy {...}

component-with-template.component.ts:

@Component({
  selector: "app-component-with-template",
  templateUrl: "./component-with-template.component.html"
})
export class ComponentWithTemplate extends BaseListComponent<DataInterface, DataService> {...}

在 Angular 中使用这样的继承是一种好习惯吗?关于性能,使用继承还是创建服务哪个更好?

【问题讨论】:

    标签: angular angular-components javascript-inheritance


    【解决方案1】:

    根据我的经验,使用继承会使调试父类变得困难,因为调试器/日志将显示给实现它的每个子类。

    我尝试远离继承,并尝试将服务或导出的函数用于一遍又一遍地完成的常见任务。该服务可以在任何需要的地方注入,并且可以在单元测试中进行模拟。这种模式就是所谓的“优先组合性优于继承”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-03
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 2012-03-11
      • 1970-01-01
      • 2014-08-27
      相关资源
      最近更新 更多