admin管理员组文章数量:1315334
I am working on a Meteor web application using Angular 2 and TypeScript. For using a REST API, I have generated client code with Swagger Codegen. Unfortunately there is no sample on GitHub, how to use the rest client.
I have an angular 2 view ponent which is injecting an Angular 2 service (ProductTreeService) and the generated API (both marked as "@Injectable"):
@Component({
selector: 'panel-product-selection',
template,
providers: [ProductTreeService, UserApi],
directives: [ProductTreeComponent]
})
export class PanelProductSelectionComponent
{
private categoriesProductsTree: Collections.LinkedList<CategoryTreeElement>;
private productTreeService: ProductTreeService;
private userApi: UserApi;
constructor(productTreeService: ProductTreeService, userApi: UserApi)
{
this.productTreeService = productTreeService;
this.userApi = userApi;
}
ngOnInit(): void
{
//...
}
}
While the angular service only is accessable and all is working fine, the application is crashing when I inject UserApi. The only difference between UserApi and ProductTreeService is the constructor: The service has no constructor parameters, the generated Api class has:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
So how can I inject the generated API?
I am working on a Meteor web application using Angular 2 and TypeScript. For using a REST API, I have generated client code with Swagger Codegen. Unfortunately there is no sample on GitHub, how to use the rest client.
I have an angular 2 view ponent which is injecting an Angular 2 service (ProductTreeService) and the generated API (both marked as "@Injectable"):
@Component({
selector: 'panel-product-selection',
template,
providers: [ProductTreeService, UserApi],
directives: [ProductTreeComponent]
})
export class PanelProductSelectionComponent
{
private categoriesProductsTree: Collections.LinkedList<CategoryTreeElement>;
private productTreeService: ProductTreeService;
private userApi: UserApi;
constructor(productTreeService: ProductTreeService, userApi: UserApi)
{
this.productTreeService = productTreeService;
this.userApi = userApi;
}
ngOnInit(): void
{
//...
}
}
While the angular service only is accessable and all is working fine, the application is crashing when I inject UserApi. The only difference between UserApi and ProductTreeService is the constructor: The service has no constructor parameters, the generated Api class has:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
So how can I inject the generated API?
Share Improve this question asked Sep 7, 2016 at 14:41 DolfDolf 1012 silver badges8 bronze badges 2- Can you please include some of the generated code? UserApi, isn't that just the sample provided? The code you linked to isn't meant to be included in projects -- it is a sample of generated code. – Martin Commented Sep 7, 2016 at 14:46
- Here is an example using Typescript-angular2 API client generated by Swagger Codegen: github./taxpon/ng2-swagger-example. Would this help? – William Cheng Commented Sep 8, 2016 at 6:55
3 Answers
Reset to default 3I would use the environment.ts file with a property for the url:
export const environment = {
production: false,
basePath: 'https://virtserver.swaggerhub./user/project/1.0.0'
};
And then in the app.module you provide the config to the service:
providers: [
{ provide: BASE_PATH, useValue: environment.basePath },
],
This works with Swagger generated code.
Hope that helps! (updated to Angular6+)
https://blog.angulartraining./how-to-manage-different-environments-with-angular-cli-883c26e99d15
After further researches I got the solution. The constructor of the rest client UserApi is expecting an HTTP provider as following:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
Coming from Java, I thought maybe this provider has to be initialized somehow in the constructor which is injecting this API. In actual fact, this initialization has to be done in the NgModule containing the injecting ponent, as described in this thread.
This solution worked for me.
Old question but ... that's how you should do it:
providers: [
{
provide: API_BASE_URL, // or BASE_PATH
useFactory: () => {
return environment.apiBaseUrl // or return just "your base url as text"
}
}]
本文标签: javascriptUsing generated Swagger TypeScript Rest Client in Angular2Stack Overflow
版权声明:本文标题:javascript - Using generated Swagger TypeScript Rest Client in Angular2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977170a2408191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论