Search code examples
angularservicebuildangular-cliproduction-environment

ERROR in : Can't resolve all parameters for Service when build with ng build --prod


DataService

import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers } from "@angular/http";
import { environment } from "../../environments/environment";
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {

  public options: RequestOptions;

  constructor(protected url: string, protected http: Http) {
  }

  getAll() {
    return this.http.get(this.url, this.options)
      .map(
      response => response.json()
      )
  }

  get(id: string) {
    return this.http.get(this.url + '/' + id, this.options)
      .map(response => {
        return response;
      })
  }

  create(resource) {
    return this.http.post(this.url, resource, this.options)
      .map(response => {
        return response;
      })
  }

  update(resource) {
    return this.http.put(this.url, resource);
  }

  delete(id: string) {
    return this.http.delete(this.url + '/' + id)
  }


}

ChildService

import { Injectable } from '@angular/core';
import { DataService } from "../data.service";
import { Http, RequestOptions } from "@angular/http";
import { environment } from "../../../environments/environment";

@Injectable()
export class ChildService extends DataService{

  constructor(http:Http) {
    super(environment.url,http);
  }

}

I am creating a service DataService which has all http methods(get,create,delete,update) and all the services which are used in the project are extending the DataService.In the constructor of DataService i am passing the http object and url from the child class by making a super(url,http) call to parent constuctor.In this way I am getting an error in the latest version of cli but not in the older versions.I am getting this error when building with ng build --prod and not getting error in ng build .can anyone help.


Solution

  • I found the problem!

    You have the DataService mark as @Injectable but the constructor has a non-injectable parameter (url: string).

    You must remove DataService from the providers list and remove the @Injectable label