Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将服务从继承的组件注入或发送到基础组件_Angular_Typescript_Oop - Fatal编程技术网

Angular 如何将服务从继承的组件注入或发送到基础组件

Angular 如何将服务从继承的组件注入或发送到基础组件,angular,typescript,oop,Angular,Typescript,Oop,我想创建baseComponent以使用所有现有组件来继承它。 现在我想知道向baseComponent注入服务的最佳方法,我有两个解决方案。 首先:将ctor的服务发送到超级组件,如下面的代码,这样会导致更改主体和方法中的所有子组件。但这种方式工作正常 伊瑟维茨酒店 export interface IService {} export interface ICrudService extends IService { getById(id: any): Promis

我想创建baseComponent以使用所有现有组件来继承它。 现在我想知道向baseComponent注入服务的最佳方法,我有两个解决方案。 首先:将ctor的服务发送到超级组件,如下面的代码,这样会导致更改主体和方法中的所有子组件。但这种方式工作正常

伊瑟维茨酒店

    export interface IService {}
    export interface ICrudService extends IService {
    getById(id: any): Promise<any>;
    getList(): Promise<any>;
    getListByParam(fields?: Array<string>,rel?: Array<ItemRelValue>,statePaging?: State,filterSingle?: Array<any>,extraParams?: any,groups?: Array<any>): Promise<any>;
    update(item: any): Promise<any>;
    remove(id: any): Promise<any>;
    add(item: any): Promise<any>;
    }
ICrudService.ts

    export interface IService {}
    export interface ICrudService extends IService {
    getById(id: any): Promise<any>;
    getList(): Promise<any>;
    getListByParam(fields?: Array<string>,rel?: Array<ItemRelValue>,statePaging?: State,filterSingle?: Array<any>,extraParams?: any,groups?: Array<any>): Promise<any>;
    update(item: any): Promise<any>;
    remove(id: any): Promise<any>;
    add(item: any): Promise<any>;
    }
第二种方法:我想使用泛型模式,或者是减少项目变更的最佳方法,从泛型模式发送服务,比如类型(我不喜欢这个模式),如下代码所示

导出类EducationalPostComponent扩展Base2{ //端区

BaseComponent.ts

    export abstract class BaseComponent{
      public gridData: any = {  };
      public state: State = {  };
      protected param: Array<string> = [];
      protected rel: Array<ItemRelValue> = [];
      protected filter: Array<any> = [];
      protected groups: Array<Group> = [];
      protected aggregate: Array<Aggregate> = [];

      constructor(public title: Title,public router: Router,public toaster?: ToasterService,public service:IService) {   }

      protected getDataByParam() {
        this.service
               .getListByParam(this.param,this.rel,this.state,this.filter,null,this.groups)
          .then(response => {
            if (response.total) {
              this.gridData.total = Number(response.total);
            }
            response.data.forEach((item, index) => {
              this.rowReview(item);
              item.rowNumber = rowNumber++;
              this.gridData.data.push(item);
            });
          })
          .catch(error => {
            this.toaster.popAsync("error", "خطا", error.message);
          });
      }

      protected abstract rowReview(item?);
      // Todo: chane top review response

      public exportToExcel = (): Promise<any> => {
        return this.service
                  .getListByParam(this.param,this.rel,this.state,this.filter,null,this.groups      )
          .then(response => {
            response.data.forEach((item, index) => {
              this.rowReview(item);
              item.rowNumber = index + 1;
            });
            return Promise.resolve(response);
          })
          .catch(error => {
            this.toaster.popAsync("error", "خطا", error.message);
            return Promise.reject({});
          });
      };
    }
    export abstract class BaseComponent<T>{
      ...
      public service :any={} as T;
      constructor(
        public title: Title,
        public router: Router,
       public toaster?: ToasterService,
       ) {
       }
       ...
导出抽象类BaseComponent{
...
公共服务:任意={}为T;
建造师(
公开名称:名称,
公共路由器:路由器,
公共烤面包机?:烤面包机服务,
) {
}
...
}

和继承的组件的

    export class EducationalPostComponent extends BaseComponent {

      constructor(
        public title: Title,
        public router: Router,
        public toaster: ToasterService,
        private fb: FormBuilder,
        private postsService: PostsService
      ) {
        super(title, router, toaster,postsService);
        }
    }
    export class EducationalPostComponent extends Base2<EducationalPostService> {
      constructor(
        public title: Title,
        public router: Router,
        public toaster: ToasterService,
        private fb: FormBuilder
      ) {
        super(title, router, toaster);
         }
    }
导出类EducationalPostComponent扩展Base2{
建造师(
公开名称:名称,
公共路由器:路由器,
公共烤面包机:烤面包机服务,
私人fb:FormBuilder
) {
超级(标题、路由器、烤面包机);
}
}
但这种方法不起作用,并且在运行时出错

我很困惑。
动态注入服务的最佳方式以及如何在parentComponent中工作。

您是否使用angular static injector?我们有不同名称的服务,如何动态注入服务?您是否使用angular static injector?我们有不同名称的服务,如何动态注入服务?