Created
December 25, 2023 05:52
-
-
Save ygweric/3b68f953e4eda180642e107d2bbfb207 to your computer and use it in GitHub Desktop.
动态加载js、css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const addStyle = (src: string) => { | |
let styles = document.createElement('link'); | |
styles.rel = 'stylesheet'; | |
styles.type = 'text/css'; | |
styles.media = 'screen'; | |
styles.href = src; | |
document.getElementsByTagName('head')[0].appendChild(styles); | |
}; | |
export const addScript = async (src: string) => { | |
let script = document.createElement('script'); | |
script.setAttribute('src', src); | |
document.body.appendChild(script); | |
return new Promise((resolve) => { | |
script.addEventListener('load', () => { | |
resolve(1); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment