如何在DarkaOnLine / L5中使用Swagger UI的自定义布局-Laravel 9中的Swagger

n9vozmp4  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(223)

我正在尝试为L5-Swagger Laravel软件包使用自定义布局。但是每次我尝试在“插件”中输入“OperationsLayout”时,它都会给出它不存在的错误。在这种情况下,我该如何使用它呢?下面是有问题的代码:

<body>
<div id="swagger-ui"></div>

<script src="{{ l5_swagger_asset($documentation, 'swagger-ui-bundle.js') }}"></script>
<script src="{{ l5_swagger_asset($documentation, 'swagger-ui-standalone-preset.js') }}"></script>

<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

<script type="text/babel">
    import React from "react"

    // Create the layout component
    class OperationsLayout extends React.Component {
        render() {
            const {
                getComponent
            } = this.props
            const Operations = getComponent("operations", true)
            return (
                <div>
                    <Operations />
                </div>
            )
        }
    }

    // Create the plugin that provides our layout component
    const OperationsLayoutPlugin = () => {
        return {
            components: {
                OperationsLayout: OperationsLayout
            }
        }
    }

    // Provide the plugin to Swagger-UI, and select OperationsLayout
    // as the layout for Swagger-UI
    SwaggerUIBundle({
        url: "{!! $urlToDocs !!}",
        plugins: [ OperationsLayoutPlugin ],
        layout: "OperationsLayout"
    })
</script>

<script>
    window.onload = function() {
        // Build a system
        const ui = SwaggerUIBundle({
            dom_id: '#swagger-ui',
            url: "{!! $urlToDocs !!}",
            operationsSorter: {!! isset($operationsSorter) ? '"' . $operationsSorter . '"' : 'null' !!},
            configUrl: {!! isset($configUrl) ? '"' . $configUrl . '"' : 'null' !!},
            validatorUrl: {!! isset($validatorUrl) ? '"' . $validatorUrl . '"' : 'null' !!},
            oauth2RedirectUrl: "{{ route('l5-swagger.'.$documentation.'.oauth2_callback', [], $useAbsolutePath) }}",

            requestInterceptor: function(request) {
                request.headers['X-CSRF-TOKEN'] = '{{ csrf_token() }}';
                return request;
            },

            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],

            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],

            layout: "StandaloneLayout",
            docExpansion : "{!! config('l5-swagger.defaults.ui.display.doc_expansion', 'none') !!}",
            deepLinking: true,
            filter: {!! config('l5-swagger.defaults.ui.display.filter') ? 'true' : 'false' !!},
            persistAuthorization: "{!! config('l5-swagger.defaults.ui.authorization.persist_authorization') ? 'true' : 'false' !!}",

        })

        window.ui = ui
    }
</script>
</body>

如何获得第二个脚本标记以获取OperationsLayout?
我尝试过的方法:我尝试使用localStorage来查看是否可以获取类变量和插件变量,以便代码的第二个js内联部分可以访问它们。

shstlldc

shstlldc1#

感谢Helen指出这一点。
我是这么做的。
我添加了Babel CDN,然后将我的内联脚本合并为一个。
最后,我为类型添加了“text/babel”。
就是这样,各位!现在可以按照Swagger的文档,在L5-Swagger的index.blade.php文件中工作了。
代码如下:

<div id="swagger-ui"></div>

<script src="{{ l5_swagger_asset($documentation, 'swagger-ui-bundle.js') }}"></script>
<script src="{{ l5_swagger_asset($documentation, 'swagger-ui-standalone-preset.js') }}"></script>

<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

<script type="text/babel">
    // Create the layout component
    class SidebarLayout extends React.Component {
        render() {
            const {
                getComponent
            } = this.props
            const StandaloneLayout = getComponent("StandaloneLayout", true)
            const Operations = getComponent("operations", true)
            return (
                <div>
                    <StandaloneLayout />
                </div>
            )
        }
    }
    
    // Create the plugin that provides our layout component
    const SidebarLayoutPlugin = () => {
        return {
            components: {
                SidebarLayout: SidebarLayout
            }
        }
    }

    window.onload = function() {
        // Build a system
        const ui = SwaggerUIBundle({
            dom_id: '#swagger-ui',
            url: "{!! $urlToDocs !!}",
            operationsSorter: {!! isset($operationsSorter) ? '"' . $operationsSorter . '"' : 'null' !!},
            configUrl: {!! isset($configUrl) ? '"' . $configUrl . '"' : 'null' !!},
            validatorUrl: {!! isset($validatorUrl) ? '"' . $validatorUrl . '"' : 'null' !!},
            oauth2RedirectUrl: "{{ route('l5-swagger.'.$documentation.'.oauth2_callback', [], $useAbsolutePath) }}",

            requestInterceptor: function(request) {
                request.headers['X-CSRF-TOKEN'] = '{{ csrf_token() }}';
                return request;
            },

            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],

            plugins: [
                SidebarLayoutPlugin,
                SwaggerUIBundle.plugins.DownloadUrl
            ],

            layout: "SidebarLayout",
            docExpansion : "{!! config('l5-swagger.defaults.ui.display.doc_expansion', 'none') !!}",
            deepLinking: true,
            filter: {!! config('l5-swagger.defaults.ui.display.filter') ? 'true' : 'false' !!},
            persistAuthorization: "{!! config('l5-swagger.defaults.ui.authorization.persist_authorization') ? 'true' : 'false' !!}",

        })

        window.ui = ui
    }
</script>

相关问题