NullInjectorError:没有字符串提供程序!在角度6

时间:2018-08-28 04:06:27

标签: angular dependency-injection

家长班

consul join 172.20.20.10

儿童班

import { BadRequestError } from './../common/bad-request-error';
import { NotFoundError } from './../common/not-found-error';
import { AppError } from './../common/app-error';
import { Http } from '@angular/http';
import { Injectable, OnInit } from '@angular/core';
import { catchError } from 'rxjs/operators';
import { throwError} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  constructor(private url: string , private http: Http) {
 }

getAll() {
 return this.http.get(this.url).pipe(catchError(this.handleError));
}

delete(id) {
  return this.http.delete(this.url + '/' + id)
   .pipe(
    catchError(this.handleError));
 }

update(resource) {
  return this.http.patch(this.url + '/' + resource.id, 
    JSON.stringify({isRead: true})).pipe(
      catchError(this.handleError));
  }

create(resource) {
 return this.http.post(this.url , JSON.stringify(resource))
   .pipe(
     catchError(this.handleError)
   );

}

 private handleError(err: Response) {
   if (err.status === 404) {
     return throwError(new NotFoundError());
 } if (err.status === 400) {
   return throwError(new BadRequestError(err.json()));
 }
   return throwError(new AppError(err));
 }
}

从子类传递字符串时遇到错误。

  

错误:StaticInjectorError(AppModule)[String]:
  StaticInjectorError(平台:核心)[字符串]:       NullInjectorError:没有字符串提供程序!       在NullInjector.push ../ node_modules/@angular/core/fesm5/core.js.NullInjector.get   (core.js:1062)       在resolveToken(core.js:1300)       在tryResolveToken(core.js:1244)       在StaticInjector.push ../ node_modules/@angular/core/fesm5/core.js.StaticInjector.get   (core.js:1141)       在resolveToken(core.js:1300)       在tryResolveToken(core.js:1244)       在StaticInjector.push ../ node_modules/@angular/core/fesm5/core.js.StaticInjector.get   (core.js:1141)       在resolveNgModuleDep(core.js:8376)       在NgModuleRef_.push ../ node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get   (core.js:9064)       在注入时(core.js:1403)

请提出如何解决以上错误的提示。

1 个答案:

答案 0 :(得分:1)

任何添加到服务angular的参数都将尝试通过DI系统注入,例如DataService考虑api服务的基类,在这种情况下,您无需使其成为injectable,因此只需删除可注射装饰器

DataService

export class DataService {

  constructor(private url: string , private http: Http) {
 }
 ...    

}