Vue3使用icon的两种方式实例
目录
- 1. 使用svg
- 2. 使用fontAwesome
- 3来源
- 4 总结
Vue3 中使用Icon的方式,fontAwesome 简单好用,但有时候缺少想要的icon。采用svg的方式,想要什么,直接下载,然后使用,种类更加的全,基本上没有不符合需求的icon,但是没有fontAwesome 相对的容易轻松,每次都要下载对应的图片。
1. 使用svg
a 下载需要使用的SVG图片,保存至 src/icons文件夹
文章图片
b 安装依赖 fs 和svg的loader
命令:
npm install svg-sprite-loader
命令:
npm install --save fs
c 创建模板文件 index.vue
文章图片
模板文件代码
.svg-icon{width: 3em; height: 3em; fill: currentColor; border: solid 2px red; vertical-align: middle; }>
d 全局注册 main.js
import { svgIcon } from './components'.component('svg-icon',svgIcon)
e 创建导入处理函数 在plugins中创建 svgBulider.js
文章图片
代码
import { readFileSync, readdirSync } from 'fs'let idPerfix = ''const svgTitle = /+].*?)>/const clearHeightWidth = /(width|height)="([^>+].*?)"/gconst hasViewBox = /(viewBox="[^>+].*?")/gconst clearReturn = /(\r)|(\n)/gfunction findSvgFile(dir) {const svgRes = []const dirents = readdirSync(dir, {withFileTypes: true})for (const dirent of dirents) {if (dirent.isDirectory()) {svgRes.push(...findSvgFile(dir + dirent.name + '/'))} else {const svg = readFileSync(dir + dirent.name).toString().replace(clearReturn, '').replace(svgTitle, ($1, $2) => {let width = 0let height = 0let content = $2.replace(clearHeightWidth,(s1, s2, s3) => {if (s2 === 'width') {width = s3} else if (s2 === 'height') {height = s3}return ''})if (!hasViewBox.test($2)) {content += `viewBox="0 0 ${width} ${height}"`}return ``}).replace('', '')svgRes.push(svg)}}return svgRes}export const svgBuilder = (path, perfix = 'icon') => {if (path === '') returnidPerfix = perfixconst res = findSvgFile(path)return {name: 'svg-transform',transformIndexHtml(html) {return html.replace('',`${res.join('')}`)}}}
f 修改配置文件,将svgBulider导入到配置文件
修改vite.config.js
import { svgBuilder } from './src/plugins/svgBuilder'; 'export default defineConfig({plugins: [vue(),//调用对应的函数 进行 svg 处理svgBuilder('./src/icons/')//对应的文件夹,否则无法找到]})
g 使用SVG
文章图片
核心部分
//name 取你的svg图片
2. 使用fontAwesome a npm 安装依赖
$ npm i --save @fortawesome/fontawesome$ npm i --save @fortawesome/vue-fontawesome$ npm i --save @fortawesome/fontawesome-free-solid$ npm i --save @fortawesome/fontawesome-free-regular$ npm i --save @fortawesome/fontawesome-free-brands
b mian.js 全局注册
//按需导入import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'import { faAd } from '@fortawesome/free-solid-svg-icons'import { faAddressBook } from '@fortawesome/free-solid-svg-icons'library.add(faAddressBook)// 全部导入import { fas } from'@fortawesome/free-solid-svg-icons'import { fab } from '@fortawesome/free-brands-svg-icons'library.add(fas,fab).component('font-awesome-icon', FontAwesomeIcon)
c 使用
//按需导入的使用//全局导入的使用
文章图片
3来源
来源fontAwesome https://www.jb51.net/article/228944.htm
来源svghttps://www.jb51.net/article/228948.htm
4 总结 确定对应的技术栈和版本,按照步骤,就没什么问题。
【Vue3使用icon的两种方式实例】到此这篇关于Vue3使用icon的两种方式的文章就介绍到这了,更多相关Vue3使用icon内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程
- 使用composer自动加载类文件
- android|android studio中ndk的使用
- 使用协程爬取网页,计算网页数据大小