我使用的是角度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/';
}
我该如何解决?
答案 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) {
}
...
}