【发布时间】:2017-09-27 03:37:44
【问题描述】:
我正在尝试在带有 TypeScript 的 angular2 项目中使用 SwaggerUI。 SwaggerUI 没有看到有 TypeScript 定义。所以我正在尝试使用 JavaScript 文件。
此项目是使用 ASP.Net Core SPA 服务模板创建的。我已将“dist”文件夹中的所有 SwaggerUI 文件添加到项目中的“wwwroot”文件夹中。
我在 _Layout.cshtml(查看页面)中的项目的“wwwroot”文件夹中引用了 SwaggerUI 的 JavaScript 文件,就像普通的 javascript 文件一样,并尝试使用 ShaggerUIBundle 对象。
_Layout.cshtml
<script src="/swagger-ui/swagger-ui-bundle.js"> </script>
<script src="/swagger-ui/swagger-ui-standalone-preset.js"> </script>
Swagger.Component.ts
//my import statements
export class SwaggerComponent implements OnInit {
SwaggerUIBundle:any;
}
现在 SwaggerUIBundle 填充了我期望的对象。但我无法在组件中的任何地方使用它。
ngOnInit(): void {
(<any>window).ui = this.SwaggerUIBundle({
url: "<url>",
dom_id: '#swagger-ui',
presets: [
this.SwaggerUIBundle.presets.apis
],
plugins: [
this.SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
}
this.SwaggerUIBundle 始终为空。
我认为这是因为实际上没有为 SwaggerUIBundle 分配任何值,尽管它已经填充了一个对象。
SwaggerUIBundle 是一个函数。我按照here 和here 的建议尝试了多种方法来分配此功能。
我尝试导入而不是引用文件。
import SwaggerUIBundle from 'path from root of the app'
由于“wwwroot”文件夹是一个虚拟文件夹,代表应用程序的根文件夹,当我尝试使用“import”导入时,TypeScript 编译器会抛出找不到文件的错误。
然后我将所有与 SwaggerUI 相关的文件移动到相应的 angular 组件文件夹并尝试从那里导入。然后我得到这个错误。
'allowJs 没有设置。
我已将“allowJs”添加到 tsconfig。错误仍然不会消失。
"compilerOptions": {
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipDefaultLibCheck": true,
"lib": [ "es6", "dom" ],
"types": [ "node" ],
"allowJs": true
}
感谢任何帮助。
【问题讨论】:
-
你是从哪里导入 SwaggerUI js 文件的?
-
为什么会出现在
this? -
(<any>window).ui = this.SwaggerUIBundle({没有任何意义。 -
SwaggerUI 要求 ui 对象必须出现在窗口上。所以我试图得到它。但是 this.SwaggerUIBundle 是 null 所以它无论如何都不起作用。
-
嗨@user3731783,你有没有让这个工作?我正在遵循与您非常相似的路径,并且无法让 TypeScript 与带有 SwaggerUIBundle 的 JavaScript 很好地配合使用。
标签: angular typescript swagger-ui