qiankun子應(yīng)用Element 圖標(biāo)不展示問(wèn)題

開(kāi)啟了 strictStyleIsolation: true樣式隔離后會(huì)導(dǎo)致圖標(biāo)消失
如下:


image.png

原因就是字體圖標(biāo)沒(méi)有被正確加載到
解決辦法:
在afterMount生命周期中查找所有的style,如果里面包含字體樣式的引入就插入到主應(yīng)用的head里面,確保加載字體圖標(biāo)文件

async afterMount(app, global) {
          const shadowRoot = document.querySelector(`[data-name="${app.name}"]`)?.shadowRoot

         if (shadowRoot) {
            shadowRoot.querySelectorAll('style').forEach(stl => {
              if (stl.textContent && /\.woff/.test(stl.textContent)) {
                // 使用正則匹配所有的 @font-face 規(guī)則
                const fontFaceMatches = [...stl.textContent.matchAll(/@font-face\s*\{([^}]+)\}/g)]

                fontFaceMatches.forEach(fontFaceMatch => {
                  // 提取每個(gè) @font-face 規(guī)則中的 font-family 屬性
                  const fontFamilyMatch = fontFaceMatch[0].match(/font-family:(.*?);/)

                  if (fontFamilyMatch && fontFamilyMatch[1]) {
                    const fontFamily = fontFamilyMatch[1].replace(/['"\s]/g, '')
                    const styleContent = fontFaceMatch[0].replace(/(\.\.\/)+/g, entry) // 替換入口路徑,

                    // 檢查 head 中是否已存在該 font-family 的 @font-face 樣式
                    const existingStyle = document.head.querySelector(`style[data-font-family="${fontFamily}"]`)
                    if (!existingStyle) {
                      // 如果沒(méi)有找到重復(fù)的樣式,則插入新的樣式
                      const st = document.createElement('style')
                      st.innerHTML = styleContent
                      st.setAttribute('data-font-family', fontFamily) // 為 style 元素添加自定義屬性
                      document.head.appendChild(st)
                    }
                  }
                })
              }
            })
          }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容