在 Angular 中对组件使用类继承是很好的做法吗?嵌套继承呢?



如果我有这样的类:

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({
selector: "app-component-with-template",
templateUrl: "./component-with-template.component.html"
})
export class ComponentWithTemplate extends BaseListComponent<DataInterface, DataService> {...}

在Angular?关于性能,使用继承还是创建服务更好?

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

我尽量远离继承,并尝试将服务或导出的函数用于反复完成的常见任务。该服务可以被注入到任何需要的地方,并且可以在单元测试中进行模拟。这种模式就是所谓的";比起继承,更喜欢可组合性";。

最新更新