我正在尝试使用openapi-generator-cli从v2扩展文件生成API客户端。为此,我使用了openapi-generator-cli的docker容器,该容器报告其版本为“ 4.1.0-SNAPSHOT”。
代码生成可使用以下选项:
{
"npmName": "...",
"npmVersion": "0.0.3",
"snapshot": true,
"ngVersion": "8.1.1"
}
并且我还尝试将providedInRoot
选项设置为true。
但是,生成的服务类未使用@Injectable
装饰器进行注释。因此,在将它们导入我的组件并在组件的构造函数中添加服务之后,我将无法使用它们。这是我的组件的样子:
import { Component, OnInit } from '@angular/core';
import { UsersService, User } from '...'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(userService: UsersService) {}
title = 'user-frontend';
ngOnInit() {
this.userService.listUsers();
}
}
失败,因为userService在AppComponent范围内不存在。
这是我导入生成的模块的方式:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ApiModule } from '...';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ApiModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
关于生成api客户端时我的错误在哪里的任何想法?
编辑: 生成的代码如下:
@Injectable({
providedIn: 'root'
})
export class UsersService {
protected basePath = 'http://localhost';
public defaultHeaders = new HttpHeaders();
public configuration = new Configuration();
public encoder: HttpParameterCodec;
constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
if (configuration) {
this.configuration = configuration;
this.configuration.basePath = configuration.basePath || basePath || this.basePath;
} else {
this.configuration.basePath = basePath || this.basePath;
}
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
}
...
}
答案 0 :(得分:2)
很多问题从'...'导入{ApiModule};生成的代码来自哪里? 您将其发布到npm并使用,还是只复制粘贴生成的代码? 试试这个
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
ApiModule.forRoot(() => {
return new Configuration({
basePath: `${environment.HOST}:${environment.PORT}`,
});
}),,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
您生成的代码应该是这样
@Injectable({
providedIn: 'root'
})
export class PetsService {
protected basePath = 'http://localhost';
public defaultHeaders = new HttpHeaders();
public configuration = new Configuration();
constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
if (configuration) {
this.configuration = configuration;
this.configuration.basePath = configuration.basePath || basePath || this.basePath;
} else {
this.configuration.basePath = basePath || this.basePath;
}
}
解决方案:在构造函数中使用私有或公共
说明: 在这里,我们和您的问题打字稿一样,都不知道您想要什么
class TestClass {
constructor(name: string) {
}
}
这是最后一个使用普通POO编程语言的示例
class TestClass {
private name: string;
constructor(name: string) {
this.name = name;
}
}
但是打字稿为我们提供了一种最小化代码的简便方法
class TestClass {
constructor(private name: string) { }
}