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

在静态模板中,脚本标记导入另一个不工作的文件

  •  0
  • Noitidart  · 技术社区  · 6 年前

    我创造了 Field.js index.js . 索引文件 使用字段。然而 JS场 从来没有加载。我的沙盒在下面。

    不可能吗 <script src="Field.js"></script> 在密码箱里?它只适用于 src="index.js" ?

    这是我的沙盒- https://codesandbox.io/s/l55933j36z

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  1
  •   Bibberty    6 年前

    下面是我们如何监听自定义事件的示例。这将允许您“等待”脚本并正常运行。

    // This lives in you scrip dependancy.
    const scriptReadyEvent = new CustomEvent('MyScriptLoaded', {
      bubbles: true
    });
    
    // Dummy a delay in loading a script
    function demoDelay() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
        resolve('foo');
        }, 2000);
      });
    }
    
    // Listen for script loaded
    document.addEventListener('MyScriptLoaded', () => {
      console.log('Script is loaded');
    });
    
    // The script would do this.
    demoDelay().then(() => {
      document.dispatchEvent(scriptReadyEvent);
    });
    推荐文章