diff --git a/components/IconFont.js b/components/IconFont.js index ddf15d48..a363eae8 100644 --- a/components/IconFont.js +++ b/components/IconFont.js @@ -10,30 +10,34 @@ export default function IconFont() { const router = useRouter() useEffect(() => { - loadExternalResource('/webfonts/iconfont.js').then(u => { - console.log('iconfont loaded') + loadExternalResource('/webfonts/iconfont.js') + .then(u => { + console.log('iconfont loaded'); - // 查找所有 标签且 class 包含 'icon-' - const iElements = document.querySelectorAll('i[class*="icon-"]'); - iElements.forEach(element => { - const className = Array.from(element.classList).find(cls => cls.startsWith('icon-')); - if (className) { - // 创建新的 元素 - const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); - svgElement.setAttribute('class', 'icon'); - svgElement.setAttribute('aria-hidden', 'true'); + // 查找所有 标签且 class 包含 'icon-' + const iElements = document.querySelectorAll('i[class*="icon-"]'); + iElements.forEach(element => { + const className = Array.from(element.classList).find(cls => cls.startsWith('icon-')); + if (className) { + // 创建新的 元素 + const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svgElement.setAttribute('class', 'icon'); + svgElement.setAttribute('aria-hidden', 'true'); - const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use'); - useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`); - svgElement.appendChild(useElement); + const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use'); + useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`); + svgElement.appendChild(useElement); - // 替换原来的 元素 - element.replaceWith(svgElement); - console.log(`Replaced with class "${className}" to `); - } + // 替换原来的 元素 + element.replaceWith(svgElement); + console.log(`Replaced with class "${className}" to `); + } + }); + }) + .catch(error => { + console.warn('Failed to load iconfont.js:', error); }); - }) - }, [router]) + }, [router]); return }