mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 15:09:22 +00:00
28 lines
645 B
JavaScript
28 lines
645 B
JavaScript
// 封装异步加载资源的方法
|
|
|
|
/**
|
|
* 加载外部资源
|
|
* @param url 地址 例如 https://xx.com/xx.js
|
|
* @param type js 或 css
|
|
* @returns {Promise<unknown>}
|
|
*/
|
|
export function loadExternalResource (url, type) {
|
|
return new Promise((resolve, reject) => {
|
|
let tag
|
|
|
|
if (type === 'css') {
|
|
tag = document.createElement('link')
|
|
tag.rel = 'stylesheet'
|
|
tag.href = url
|
|
} else if (type === 'js') {
|
|
tag = document.createElement('script')
|
|
tag.src = url
|
|
}
|
|
if (tag) {
|
|
tag.onload = () => resolve(url)
|
|
tag.onerror = () => reject(url)
|
|
document.head.appendChild(tag)
|
|
}
|
|
})
|
|
}
|