github链接到我的回购here
相关代码:
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
@Injectable()
export class DataService {
constructor (private url: string, private http: Http) { }
getAll() {
return this.http.get(this.url)
.map(response => response.json())
.catch(this.errorHandler);
}
private errorHandler(error: Response) {
// tslint:disable-next-line:curly
if (error.status === 400)
return Observable.throw(new BadInputError(error));
// tslint:disable-next-line:curly
else if (error.status === 404)
return Observable.throw(new NotFoundError(error));
return Observable.throw(new AppError(error));
}
}
我发现similar question表示问题与覆盖错误处理有关,但它并没有真正建议解决方案。
当我运行ng serve --prod
或ng build --prod
时,我收到以下异常:
ERROR in Error: Can't resolve all parameters for DataService in C:/Users/Patrick/Desktop
/Angular/trial-app/src/app/services/data.service.ts: (?, [object Object]).
at Error (native)
at syntaxError (C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angular\com
piler\bundles\compiler.umd.js:1729:34)
at CompileMetadataResolver._getDependenciesMetadata (C:\Users\Patrick\Desktop\Angula
r\trial-app\node_modules\@angular\compiler\bundles\compiler.umd.js:15816:35)
at CompileMetadataResolver._getTypeMetadata (C:\Users\Patrick\Desktop\Angular\trial-
app\node_modules\@angular\compiler\bundles\compiler.umd.js:15684:26)
at CompileMetadataResolver._getInjectableMetadata (C:\Users\Patrick\Desktop\Angular\
trial-app\node_modules\@angular\compiler\bundles\compiler.umd.js:15670:21)
at CompileMetadataResolver.getProviderMetadata (C:\Users\Patrick\Desktop\Angular\tri
al-app\node_modules\@angular\compiler\bundles\compiler.umd.js:15961:40)
at C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angular\compiler\bundles
\compiler.umd.js:15890:49
at Array.forEach (native)
at CompileMetadataResolver._getProvidersMetadata (C:\Users\Patrick\Desktop\Angular\t
rial-app\node_modules\@angular\compiler\bundles\compiler.umd.js:15850:19)
at CompileMetadataResolver.getNgModuleMetadata (C:\Users\Patrick\Desktop\Angular\tri
al-app\node_modules\@angular\compiler\bundles\compiler.umd.js:15505:50)
at addNgModule (C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angular\com
piler\bundles\compiler.umd.js:24268:58)
at C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angular\compiler\bundles
\compiler.umd.js:24279:14
at Array.forEach (native)
at _createNgModules (C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angula
r\compiler\bundles\compiler.umd.js:24278:26)
at analyzeNgModules (C:\Users\Patrick\Desktop\Angular\trial-app\node_modules\@angula
r\compiler\bundles\compiler.umd.js:24153:14)
at analyzeAndValidateNgModules (C:\Users\Patrick\Desktop\Angular\trial-app\node_modu
les\@angular\compiler\bundles\compiler.umd.js:24163:35)
答案 0 :(得分:1)
将此添加为评论,但没有足够的代表。但是,我同意trichetriche,我怀疑一个字符串不算作依赖注入,从而绊倒你。检查此处给出的答案:Angular 2 passing parameters to constructor throws DI exception
就个人而言,我已经将服务用作一种全局变量来实现这一目标。例如:
{{1}}