本文地址:https://www.cnblogs.com/veinyin/articles/14631591.html
-
按照 svg 文件內容,將 path 拼接進去,如果要定義顏色,可以把 fill 設置為需要的顏色
-
上述字符串通過 js-base64 編碼為 base64
-
使用 taro 的 Image,src 設置為剛剛得到的 base64 串,在 image 上設置大小
1 export default function SvgIcon(props) { 2 const { path = '', color = '#5B6FFF', size = 20 } = props 3 if (!path) return 4 const svg = `<?xml version="1.0" standalone="no"? 5 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 7 <svg t="1616385809871" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9443" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20">
<defs><style type="text/css"></style></defs>
<path d="${path}" p-id="9444" fill="${color}"></path>
</svg>` 8 const imgSrc = `data:image/svg+xml;base64,${Base64.encode(svg)}` 9 return ( 10 imgSrc && ( 11 <Image style={{ width: size, height: size }} src={imgSrc} /> 12 ) 13 ) 14 }
