首页 > 解决方案 > 再次加载内容时,与 Fetched 部分一起动态加载的 ES6 模块不会执行

问题描述

我有一个使用 fetch API 切换内容的页面。每个内容都附带一个 js 文件。其中一些是 type='module'。然而,尽管模块在第一次加载期间运行良好,但当我切换内容(调用不同的 fetch 端点)并再次切换回来时,相应的脚本似乎不起作用。当整个页面发生变化时(例如,进入个人资料页面,然后再次进入主页),脚本正常工作。

---在主页内调用内容的部分---

...
    auth.fetch(partUri).then((resp) => {
            return resp.text().then(text => {
                
                $('.title > h2').text(titles[ch - 1]);

                $('#part-' + partNo).html(text);
                }
    
            });
          
    });
...

--- 在内容的 html 文件中 ---

...
<script> <--- this script is working after switching back
    new SimpleLightbox('.gallery-item a', {
        disableScroll: false
    });
</script>
<script type="module" src="js/part-3.js"></script> <--- this script is working only the first time

标签: javascriptfetches6-modules

解决方案


推荐阅读