无法解析DataService的所有参数?

时间:2018-03-13 04:32:14

标签: angular

我使用的是角度CLI版本1.6.7和节点版本8.9.4 当我正在运行ng serve时,它不会给出任何错误。 但是当我尝试使用ng build -prod打包它时,它会给出错误

``ERROR in : Can't resolve all parameters for DataService in C:/Users/user/Desktop/Projects/Hocr/project/hocr-ui/src/app/services/data/data.service.ts: (?, [object Object])``

下面是我的data.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http/';

@Injectable()
export class DataService {

  constructor(private endpoint: string, private http: Http) { }

  getAll() {
    return this.http.get(this.endpoint);
  }

  create( resource ) {
    return this.http.post(this.endpoint, JSON.stringify(resource));
  }

  update( resource ) {
    return this.http.put(this.endpoint + resource.id ,JSON.stringify(resource));
  }

  delete(id) {
    return this.http.delete(this.endpoint + id);
  }
}
下面的

是我的user.service.ts

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

@Injectable()
export class UserService extends DataService {

  constructor(http: Http) {
    super(AppSettings.USER_ENDPOINT, http);
  }
}

下面是我的app.settings.ts

export class AppSettings {
    public static API_ENDPOINT = 'http://localhost:8080';
    public static USER_ENDPOINT = AppSettings.API_ENDPOINT + '/api-user/';
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您可以将endpoint绑定到值提供者:

export const EndpointToken = new InjectionToken<string>("My Endpoint");

@NgModule({
    ...
    import: [ HttpClientModule ],
    providers: [
        DataService,
        { provide: EndpointToken, useValue: AppSettings.USER_ENDPOINT }
    ]
})
...

然后在DataService中使用带有InjectionToken的@Inject装饰器进行设置:

@Injectable()
export class DataService {

  constructor(@Inject(EndpointToken) private endpoint: string, private http: Http) { 
  }
  ...
}