簡體   English   中英

在靜態模板中,腳本標記用於導入另一個文件不起作用

[英]In static template, script tag to import another file not working

我已經創建了Field.jsindex.js index.js使用Field。 但是, Field.js從未加載。 我的沙箱在下面。

是否無法在codesandbox中執行<script src="Field.js"></script> 它僅適用於src="index.js"嗎?

這是我的沙箱-https: //codesandbox.io/s/l55933j36z

在此處輸入圖片說明

這是我們如何監聽自定義事件的示例。 這將允許您“等待”腳本並正確運行。

 // 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); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM