简体   繁体   中英

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

I have created Field.js and index.js . index.js uses Field. However Field.js is never loading. My sandbox is below.

Is it not possible to do <script src="Field.js"></script> in codesandbox? Does it only work for src="index.js" ?

Here is my sandbox - https://codesandbox.io/s/l55933j36z

在此处输入图片说明

Here is a sample of how we might listen for a custom event. This would allow you to 'wait' for the script and function correctly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM