Reputation: 1065
github link to my repo here
relevant code:
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));
}
}
I've found a similar question which indicates that the problem has to do with overriding error-handling, but it doesn't really suggest a solution.
When I run either ng serve --prod
or ng build --prod
I get the following exception(s):
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)
Upvotes: 0
Views: 788
Reputation: 30
Would add this as a comment, but don't have enough rep. However, I agree with trichetriche, I suspect that a string is not counted as a dependency injection and thus tripping you up. Check the answer given here: Angular 2 passing parameters to constructor throws DI exception
Personally, I've used services as a sort of global variable to achieve this. For example:
//in another class called URL-Service.ts you will need to add this
//to providers at the root level in app.module
export class URLService {
public savedURL: string = '';
}
//in another class where you have access to the url
export class SomeClass {
constructor(private urlService = URLService){}
someMethod(){
this.urlService.savedURL = whateverHasTheURL;
}
}
export class DataService {
constructor (private urlService = URLService, private http: Http) {
this.url = this.urlService.savedURL;
}
...
}
Upvotes: 1