首页 文章

NullInjectorError:没有String的提供者!在角6

提问于
浏览
2

家长班

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

儿童班

import { DataService } from './data.service';
 import { Http } from '@angular/http';
 import { Injectable } from '@angular/core';

@Injectable({
 providedIn: 'root'
})
export class PostService extends DataService {
  constructor(http: Http) {
    super('https://jsonplaceholder.typicode.com/posts' , http);
 }

}

从子类传递字符串时出现以下错误 .

错误:StaticInjectorError(AppModule)[String]:StaticInjectorError(Platform:core)[String]:NullInjectorError:没有String的提供者! atInullInjector.push ../ node_modules/@angular/core/fesm5/core.js.NullInjector.get(core.js:1062)at resolveToken(core.js:1300)at tryResolveToken(core.js:1244)at StaticInjector在StaticInjector.push的tryResolveToken(core.js:1244)的resolveToken(core.js:1300)处的.push ../ node_modules/@angular/core/fesm5/core.js.StaticInjector.get(core.js:1141)在NgModuleRef_.push ../ node_modules / @ angular / core / fesm5的resolveNgModuleDep(core.js:8376)中的../node_modules/@angular/core/fesm5/core.js.StaticInjector.get(core.js:1141) /core.js.NgModuleRef_.get(core.js:9064)在inject(core.js:1403)

请建议如何解决上述错误 .

1 回答

  • 1

    添加到服务角度的任何参数都会尝试通过DI系统注入此内容,考虑到api服务的基类,在这种情况下你不需要使它 injectable 所以只需删除注入装饰器

    DataService的

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

相关问题