首页 > 解决方案 > 如何将百里香中的javascript外部化为片段?

问题描述

我想创建一个可重用的 javascript 片段,然后我可以简单地包含在<th:block layout:fragment="script">块中,如下所示:

<th:block layout:fragment="script">
    <div th:replace="test :: script"></div>
</th:block>

test/script.html

<html xmlns="http://www.w3.org/1999/html" xmlns:th="http://www.thymeleaf.org">
    <body>
        <div th:fragment="script">
            <script type="text/javascript">
                $(document).ready(function(){
                    alert('test alert');
                });
            </script>
        </div>
    </body>
</html>

当我直接使用jsin 代替th:replace语句时,警报会执行。当我尝试包含上面写的片段时,什么也没有发生。

问题:我怎么知道thymeleaf包含外部化的js?

标签: javascriptthymeleaf

解决方案


我不得不将外部替换<div><script>

<th:block layout:fragment="script"> < script th:replace="external :: script"> </th:block>

并且:

<脚本th:fragment="脚本">


推荐阅读