首页 > 解决方案 > Can't resolve all parameters for DataService in D:/appangular/src/app/services/data.service.ts: (?, [object Object]) 中的错误

问题描述

当我运行ng build --prod我得到这个错误。

数据服务.ts

import { BadInput } from './../common/bad-input';
import { AppError } from './../common/app-error';
import { Injectable } from '@angular/core';
import { NotFoundError } from '../common/not-found-error';
import { throwError } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map } from 'rxjs/operators';

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

  constructor(private url : string ,private http : HttpClient) { }

  getAll(){
    return this.http.get(this.url).
    pipe(map(response => response),
    catchError(
      this.handleError
    )         
    )} 

  create(resource){
    return this.http.post(this.url,resource).
    pipe(
      catchError(
        this.handleError
      )
    )}

  update(resource){
    return this.http.put(this.url,resource).
    pipe(
      catchError(
        this.handleError
      )
    )}

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

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

标签: angulartypescript

解决方案


也许你需要这个?

import { BadInput } from './../common/bad-input';
import { AppError } from './../common/app-error';
import { Injectable } from '@angular/core';
import { NotFoundError } from '../common/not-found-error';
import { throwError } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map } from 'rxjs/operators';

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

  constructor(private http : HttpClient) { }

  getAll(url : string){
    return this.http.get(url).
    pipe(map(response => response),
    catchError(
      this.handleError
    )         
    )} 

  create(url : string, resource){
    return this.http.post(url,resource).
    pipe(
      catchError(
        this.handleError
      )
    )}

  update(url : string, resource){
    return this.http.put(url,resource).
    pipe(
      catchError(
        this.handleError
      )
    )}

  delete(url : string, resource){
    return this.http.delete(url+'/'+resource.id).
    pipe(
      catchError(
        this.handleError
      )
    )}

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

推荐阅读