Dol*_*olf 5 javascript swagger meteor typescript angular
我正在使用Angular 2和TypeScript处理Meteor Web应用程序.对于使用REST API,我使用Swagger Codegen生成了客户端代码.不幸的是,GitHub上没有样本,如何使用其余的客户端.
我有一个角度2视图组件,它注入一个Angular 2服务(ProductTreeService)和生成的API(都标记为"@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
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
虽然角度服务只是可访问的并且一切正常,但是当我注入UserApi时应用程序崩溃了.UserApi和ProductTreeService之间的唯一区别是构造函数:服务没有构造函数参数,生成的Api类具有:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
Run Code Online (Sandbox Code Playgroud)
那么我该如何注入生成的API呢?
经过进一步研究,我得到了解决方案。其余客户端UserApi的构造函数需要 HTTP 提供程序,如下所示:
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
Run Code Online (Sandbox Code Playgroud)
来自 Java,我想也许这个提供者必须在注入这个 API 的构造函数中以某种方式初始化。事实上,这个初始化必须在包含注入组件的 NgModule 中完成,如本线程中所述。
这个解决方案对我有用。
| 归档时间: |
|
| 查看次数: |
4302 次 |
| 最近记录: |