angular框架中提取公共服务及公用方法的实现

公共服务的提取

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;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值