代码之家  ›  专栏  ›  技术社区  ›  PatS

SwaggerUIBundle和SwaggerUi有什么区别

  •  2
  • PatS  · 技术社区  · 7 年前

    我在我发现的样本中看到了这两种情况,但没有看到它们有什么不同。如果您仅在HTML页面中使用此应用程序(而不是使用单个页面应用程序),是否需要捆绑包?如果您使用单页面应用程序,是否需要该捆绑包?

    这个 Swagger UI docs

    我见过一些例子 like this one 其中SwaggerUIBundle用于tomcat(python或其他web服务器)示例中的web页面。

    <script src="./swagger-ui-bundle.js"> </script>
    // later
    <script>
    window.onload = function() {
      // Build a system
      const ui = SwaggerUIBundle({
        url: "https://petstore.swagger.io/v2/swagger.json",
    

    但也看到了 examples like this 用招摇过市的人。

    window.swaggerUi = new SwaggerUi({
      url: "http://petstore.swagger.wordnik.com/api/api-docs",
      dom_id: "swagger-ui-container",
    

    2 回复  |  直到 7 年前
        1
  •  10
  •   PatS    6 年前

    本页 Installation Distribution Channels NPM Registry

    然后解释了它们之间的区别。因此,它们在功能上是等价的,但您选择的将取决于您的web服务器/网站如何提供 swagger用户界面页面

    第一个例子 const ui = SwaggerUIBundle(... 适用于Swagger UI 3.x,这是Swagger UI的当前版本。第二个例子 window.swaggerUi = new SwaggerUi(... 信用卡 @Helen 获取此信息 this 回答)

    大摇大摆解释说

    SwaggerUI用于可以导入npm模块的应用程序中。 这包括React、Angular或其他单页应用程序(spa),这些应用程序包括类似于webpack的工具,用于将资源打包以交付给浏览器。

    网页上写着:

    import SwaggerUI from 'swagger-ui'

    swaggerui是指JavaScript web项目使用的,这些项目包括Webpack、Browserify和Rollup等模块绑定器。

    下面是一个使用npm内置模块的示例 swagger-ui

    import SwaggerUI from 'swagger-ui'
    // or use require, if you prefer
    const SwaggerUI = require('swagger-ui')
    SwaggerUI({
      dom_id: '#myDomId'
    })
    

    大摇大摆地解释道

    当你的应用不支持导入npm模块(例如java webapp)时,使用SwaggerUIBundle。

    swagger用户界面可以通过使用 大摇大摆索引.html页码

    如何使用SwaggerUIBundle的示例如下:

    var SwaggerUIBundle = require('swagger-ui-dist').SwaggerUIBundle
    const ui = SwaggerUIBundle({
        url: "https://petstore.swagger.io/v2/swagger.json",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "StandaloneLayout"
      })
    

    如果您所在的JavaScript项目无法处理传统的npm模块,那么可以执行以下操作:

    var SwaggerUIBundle = require('swagger-ui-dist').SwaggerUIBundle
    

    它使用require()哪个 包含捆绑包的方式。

    一种不那么令人困惑的解释方法是:

    招摇过市用户界面 ).

    const ui = SwaggerUIBundle({
        url: "https://petstore.swagger.io/v2/swagger.json",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "StandaloneLayout"
      })
    

    <script src="bundle.js"></script>. 看到了吗 https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html (位于swagger ui/dist中/索引.html).

    var SwaggerUIBundle=require('swagger-ui-dist')。SwaggerUIBundle
    

    如何将javscript打包到页面上取决于您自己。

        2
  •  2
  •   Helen    7 年前

    const ui = SwaggerUIBundle(... 是为了 ,这是当前版本的Swagger UI。

    window.swaggerUi = new SwaggerUi(... Swagger UI 2.x版

    看到了吗 here