Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Javascript 角度,无法在父类中注入服务_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度,无法在父类中注入服务

Javascript 角度,无法在父类中注入服务,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个为ngx toastr创建的包装烤面包机服务。我可以将其注入一个派生类,但不能注入父类。在父类中,它是未定义的 toastr.service.ts import { Injectable } from '@angular/core'; import { ToastrService, IndividualConfig } from 'ngx-toastr'; @Injectable() export class MyToasterService {

我有一个为ngx toastr创建的包装烤面包机服务。我可以将其注入一个派生类,但不能注入父类。在父类中,它是未定义的

toastr.service.ts

   import { Injectable } from '@angular/core';
    import { ToastrService, IndividualConfig } from 'ngx-toastr';
    
    @Injectable()
    export class MyToasterService {
    
        options: IndividualConfig;
    
        constructor(
            private toastr: ToastrService
        ) {
            this.options = this.toastr.toastrConfig;
            this.options.positionClass = 'toast-center-center';
            this.options.timeOut = 3000;
            this.options.closeButton = true;
        }
    
        showErrorToast() {
            let title = 'Technical Error'
            let message = "We are facing some technical issues. Please try agian later"
            this.toastr.error(message, title, this.options);
        }
    
    }
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { technicalErrorMessage, forbiddenMessage } from './message';
import { MyToasterService } from '../../common/toastr-service/toastr.service'


@Injectable({
  providedIn: 'root'
})
export class BaseService {

  constructor(private toastrService:MyToasterService) {
  
   }

  errorInfo = new Subject();
  errors = [];

  updateErrorDetails(errorInfo, title) {
    const errorDetails: any = {
      id: title,
      type: 'error',
      position: 'top-center',
      noTimeout: true
    };

    let errorMessage = '';

    if (errorInfo.status !== 412) {
      errorMessage += `${title} : ${technicalErrorMessage}`;
      errorDetails.msg = errorMessage;
      // write here a error message ui
    } else {
      errorMessage = forbiddenMessage;
      errorDetails.msg = errorMessage;
      // write here a error message ui
    }

  }

  displayServerError(message, title) {
    const errorDetails: any = {
      id: title,
      type: 'error',
      position: 'top-center',
      noTimeout: true
    };

    let errorMessage = '';
    errorMessage += `${title} : ${message}`;
    errorDetails.msg = errorMessage;
    this.toastrService.showErrorToast(); //here this.toastrService comes as undefined
    
  }

}
base.service.ts

   import { Injectable } from '@angular/core';
    import { ToastrService, IndividualConfig } from 'ngx-toastr';
    
    @Injectable()
    export class MyToasterService {
    
        options: IndividualConfig;
    
        constructor(
            private toastr: ToastrService
        ) {
            this.options = this.toastr.toastrConfig;
            this.options.positionClass = 'toast-center-center';
            this.options.timeOut = 3000;
            this.options.closeButton = true;
        }
    
        showErrorToast() {
            let title = 'Technical Error'
            let message = "We are facing some technical issues. Please try agian later"
            this.toastr.error(message, title, this.options);
        }
    
    }
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { technicalErrorMessage, forbiddenMessage } from './message';
import { MyToasterService } from '../../common/toastr-service/toastr.service'


@Injectable({
  providedIn: 'root'
})
export class BaseService {

  constructor(private toastrService:MyToasterService) {
  
   }

  errorInfo = new Subject();
  errors = [];

  updateErrorDetails(errorInfo, title) {
    const errorDetails: any = {
      id: title,
      type: 'error',
      position: 'top-center',
      noTimeout: true
    };

    let errorMessage = '';

    if (errorInfo.status !== 412) {
      errorMessage += `${title} : ${technicalErrorMessage}`;
      errorDetails.msg = errorMessage;
      // write here a error message ui
    } else {
      errorMessage = forbiddenMessage;
      errorDetails.msg = errorMessage;
      // write here a error message ui
    }

  }

  displayServerError(message, title) {
    const errorDetails: any = {
      id: title,
      type: 'error',
      position: 'top-center',
      noTimeout: true
    };

    let errorMessage = '';
    errorMessage += `${title} : ${message}`;
    errorDetails.msg = errorMessage;
    this.toastrService.showErrorToast(); //here this.toastrService comes as undefined
    
  }

}
child.service.ts(扩展基本服务)

从'@angular/core'导入{Injectable};
从'rxjs'导入{Subject};
从“../api/api.service”导入{APIService};
从'@angular/common/http'导入{HttpClient};
从'@ngrx/Store'导入{Store};
从“../store/app.store”导入{AppState};
从“../base service/base services.service”导入{BaseService};
从“../../common/toastr-service/toastr.service”导入{MyToasterService}”
@注射的({
providedIn:'根'
})
导出类ChildService扩展了BaseService{
url:any={
someUrl:“/api/data service/”,
};
isError=新主题();
构造函数(私有api:APIService、私有http:HttpClient、,
私人商店:商店,私人ToasterService 1:MyToasterService){
超级();
}
getData(键:字符串):void{
this.http.get(this.url.someUrl).subscribe((详细信息:any)=>{
如果(详细信息、状态){
//数据使用
}
},错误=>{
this.displayServerError('ds','ds');//调用父方法,并且在其中this.toastrService不工作
this.toastrService1.batherrortoos();//这很有效
});
}
}

因此,基本上我可以在子类(扩展base.service.ts)中注入toastrService并查看toastr。但是当我注入base.service.ts时,toastr服务变得不明确了。有多个子服务类扩展基本服务类。所以我不想在每个服务类中注入toastr服务

依赖注入不是以这种方式继承的,而且它必须标记为public才能被继承。为了使Angular的DI系统能够正常工作,您必须将toastrService从子组件传递到super的构造函数

constructor(public toastrService:ToasterService) {
super(toastrService)
}

您是否尝试过
super(ToastrService 1)
?是的。这行得通。