mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-08 23:16:54 +00:00
封装loadResources方法
This commit is contained in:
27
lib/utils.js
Normal file
27
lib/utils.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// 封装异步加载资源的方法
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载外部资源
|
||||||
|
* @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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
import CONFIG_NEXT from '../config_next'
|
import CONFIG_NEXT from '../config_next'
|
||||||
|
import { loadExternalResource } from '@/lib/utils'
|
||||||
|
|
||||||
let hasLoad = false
|
let hasLoad = false
|
||||||
export default function Live2D () {
|
export default function Live2D () {
|
||||||
@@ -29,24 +30,3 @@ function initLive2D () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 封装异步加载资源的方法
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
|
import { loadExternalResource } from '@/lib/utils'
|
||||||
|
|
||||||
export default function Live2DWife () {
|
export default function Live2DWife () {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -17,27 +18,6 @@ function initLive2DWife () {
|
|||||||
const live2dPath = 'https://cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/'
|
const live2dPath = 'https://cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/'
|
||||||
// const live2d_path = "/live2d-widget/";
|
// const live2d_path = "/live2d-widget/";
|
||||||
|
|
||||||
// 封装异步加载资源的方法
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加载 waifu.css live2d.min.js waifu-tips.js
|
// 加载 waifu.css live2d.min.js waifu-tips.js
|
||||||
if (screen.width >= 768) {
|
if (screen.width >= 768) {
|
||||||
Promise.all([
|
Promise.all([
|
||||||
|
|||||||
Reference in New Issue
Block a user