公共服务的提取
ListServiceInit 服务类:可以通过Injector来检索出全局注册的服务
import { ChangeDetectorRef, Injector } from "@angular/core";
import { LoggerFactory } from "app/core/logger-factory.service";
import { Logger } from "app/core/logger.service";
import { BsModalService } from "ngx-bootstrap";
export class ListServiceInit {
log: Logger;
public loggerFactory: LoggerFactory;
public changeDetectorRef: ChangeDetectorRef;
public modalService: BsModalService;
constructor(injector: Injector) {
this.loggerFactory = injector.get(LoggerFactory);
this.changeDetectorRef = injector.get(ChangeDetectorRef);
this.modalService = injector.get(BsModalService);
this.log = this.loggerFactory.getLogger();
}
}
公共方法的提取
ListCommonInit 公共方法父类:可以将公共方法提取到父类,继承服务类
import { AfterViewInit, Injector, OnInit, ViewChild } from "@angular/core";
import { DatatableComponent } from "@swimlane/ngx-datatable";
import { Logger } from "app/core/logger.service";
import { NcQuery } from "app/models/page-request";
import { ListServiceInit } from "./list-services-init";
export class ListCommonInit extends ListServiceInit implements OnInit,AfterViewInit {
loading:boolean = false;
count: number;
log: Logger;
ncQuery: any = new NcQuery()
@ViewChild(DatatableComponent) datatable: DatatableComponent;
constructor(
public queryObMap: any,
injector: Injector
) {
super(injector);
}
ngOnInit() {
this.ncQuery.filterDatas = this.queryObMap;
}
ngAfterViewInit() {
this.changeDetectorRef.detectChanges();
}
// 分页
changeSize(event: any) {
this.datatable.limit = event.size;
}
// 重置
deleteAll() {
this.ncQuery.executeReSet();
}
}
组件类
组件类继承公共方法父类,实现将公共方法和公共服务挂载到子类的原型上
import { ChangeDetectorRef, Component, OnInit,AfterViewInit, ViewChild, Injector } from '@angular/core';
import { QueryObMap } from '../../shared/models/etc-list.params';
import { cloneDeep } from 'lodash';
import { ListCommonInit } from 'app/vha/vha-shared/models/list-common-init';
import { OrderListService } from 'app/vha/order-list/shared/services/order-list.service';
@Component({
selector: 'app-etc-list',
templateUrl: './etc-list.component.html',
styleUrls: ['./etc-list.component.scss']
})
export class EtcListComponent extends ListCommonInit {
EtcList: Array<any> = [];
constructor(
injector: Injector
) {
super(QueryObMap,injector);
}
GetEtcList(event: any) {
this.loading = true;
// this.OrderListService
// .WorkOrderList(event)
// .finally(() => (this.loading = false))
// .subscribe(
// (response) => {
// // this.ChildOrderList = response.rows;
// this.datatable.count = response.rows.length;
// // this.count = response.total;
// },
// (error) => {
// this.log.error('子单信息获取失败');
// }
// );
this.loading = false;
}
}