コンテンツにスキップ

カスタムレイアウトの作成

レイアウトは、Swagger UI がアプリケーション全体のルートコンポーネントとして使用する特別なタイプのコンポーネントです。カスタムレイアウトを定義することで、ページに表示される内容を高度に制御できます。

デフォルトでは、Swagger UI はアプリケーションに組み込まれている BaseLayout を使用します。layout パラメーターとしてレイアウト名を Swagger UI に渡すことで、別のレイアウトを指定できます。カスタムレイアウトを Swagger UI のコンポーネントとして必ず提供してください。


たとえば、操作のみを表示するカスタムレイアウトを作成したい場合は、OperationsLayout を定義できます。

1
import React from "react"
2
3
// Create the layout component
4
class OperationsLayout extends React.Component {
5
render() {
6
const {
7
getComponent
8
} = this.props
9
10
const Operations = getComponent("operations", true)
11
12
return (
13
<div className="swagger-ui">
14
<Operations />
15
</div>
16
)
17
}
18
}
19
20
// Create the plugin that provides our layout component
21
const OperationsLayoutPlugin = () => {
22
return {
23
components: {
24
OperationsLayout: OperationsLayout
25
}
26
}
27
}
28
29
// Provide the plugin to Swagger-UI, and select OperationsLayout
30
// as the layout for Swagger-UI
31
SwaggerUI({
32
url: "https://petstore.swagger.io/v2/swagger.json",
33
plugins: [ OperationsLayoutPlugin ],
34
layout: "OperationsLayout"
35
})

デフォルトレイアウトの拡張

BaseLayout を置き換えるのではなく、BaseLayout をカスタムレイアウトに組み込んで使用することもできます。

1
import React from "react"
2
3
// Create the layout component
4
class AugmentingLayout extends React.Component {
5
render() {
6
const {
7
getComponent
8
} = this.props
9
10
const BaseLayout = getComponent("BaseLayout", true)
11
12
return (
13
<div>
14
<div className="myCustomHeader">
15
<h1>I have a custom header above Swagger-UI!</h1>
16
</div>
17
<BaseLayout />
18
</div>
19
)
20
}
21
}
22
23
// Create the plugin that provides our layout component
24
const AugmentingLayoutPlugin = () => {
25
return {
26
components: {
27
AugmentingLayout: AugmentingLayout
28
}
29
}
30
}
31
32
// Provide the plugin to Swagger-UI, and select AugmentingLayout
33
// as the layout for Swagger-UI
34
SwaggerUI({
35
url: "https://petstore.swagger.io/v2/swagger.json",
36
plugins: [ AugmentingLayoutPlugin ],
37
layout: "AugmentingLayout"
38
})
© . This site is unofficial and not affiliated with Swagger.