简体   繁体   English

如何在 Typescript (Angular) 中导入 swagger model 桶文件

[英]How to import swagger model barrel file in Typescript (Angular)

We are currently automatically generating our API models with swagger and want to import them convenient into our project.我们目前正在使用 swagger 自动生成我们的 API 模型,并希望将它们方便地导入到我们的项目中。

At the moment we import it in the following way: tsconfig.ts目前我们通过以下方式导入它:tsconfig.ts

"paths": {
  "apimodel/*": ["frontend/swagger-api-definition/model/*"]
}

Random data-source class:随机数据源 class:

import { ExampleDtoQuery } from 'apimodel/exampleDtoQuery ';
import { ExampleUserDtosUserManagerDashboardResponseUserProject } from 'apimodel/exampleUserDtosUserManagerDashboardResponseUserProject ';

Names are just random, do not judge them please:)名字只是随机的,请不要评判他们:)

Now what I would like to do, is to create an alias in the tsconfig and use the barrel file, which is generated in the root.现在我想做的是在 tsconfig 中创建一个别名并使用在根目录中生成的桶文件。 Like so: tsconfig.ts像这样:tsconfig.ts

"paths": {
  "@apimodel": ["frontend/swagger-api-definition/index.ts"]
},

Random data-source class:随机数据源 class:

import { ExampleDtoQuery } from '@apimodel';
import { ExampleUserDtosUserManagerDashboardResponseUserProject } from '@apimodel ';

But the compiler keeps telling me: Cannot find module '@apimodel'.ts(2307)但是编译器一直告诉我: Cannot find module '@apimodel'.ts(2307)

Any advice?有什么建议吗?

The * in the path definition tells the TS compiler to allow imports from anything with the prefix @apimodel/ .路径定义中的*告诉 TS 编译器允许从前缀为@apimodel/的任何内容导入。 You also need to provide a baseUrl or it the TS compiler will not be able to find the barrel file in question.您还需要提供一个baseUrl ,否则 TS 编译器将无法找到有问题的桶文件。 To make it work the way you want you should be able to update the paths and baseUrl definitions to be:为了使其按您希望的方式工作,您应该能够将pathsbaseUrl定义更新为:

"baseUrl": "./",
"paths": {
  "@apimodel": ["frontend/swagger-api-definition/index.ts"]
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM