Vue.js 中怎么實現一個圖標選擇組件,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
背景
最近項目中在做一個自定義菜單需求,其中有一個為菜單設置小圖標的功能,就是大家常見的左側菜單
設置圖標不難,方案就是字體圖標,可供使用的圖標庫也有很多,比如阿里巴巴的 Iconfont,以及 Fontaswsome 等,問題在于如何優雅的提供幾百個圖標供用戶選擇,而不需要開發去一個一個的寫標簽,也不需要一個個的去找圖標。
字體圖標庫 Fontawesome 方案
我們使用字體圖標的方式,一般是一個 <i class="iconfont icon-home"></i>
這樣的標簽,平常開發中用一些圖標都是用到一個寫一個,展示10個圖標,就要寫10個標簽。
在項目中本人使用的是 Fontawesome 圖標庫方案,使用它是因為提供的可用圖標比較豐富,基本上不需要特意去找合適的圖標,直接把它的圖標庫下載過來,免費的有800多個。
這么多圖標難道要一個一個手寫800多個 i
標簽嗎?三連拒絕!
Fontawesome 下載后的文件中提供一個 svg格式的精靈圖,這個非常人性化,用 VSCode 打開這個SVG文件
可以看到是熟悉的DOM,因為SVG本質上就是一個XML,既然是DOM,那么祭出JS大法吧,用瀏覽器打開這個SVG文件,在控制臺編寫如下代碼獲取所有的圖標名稱:
const nodeArray = Array.from(document.querySelectorAll('symbol')); const names = nodeArray.map(item => item.id) names.toString()
Icons組件
大??梢院雎?/blockquote>拿到了所有圖標的 name 那就好辦了,一個數組循環唄。先別急著寫代碼,我們的目的是封裝成組件復用,那么先創建一個 Icons 組件
提供一個篩選框,然后給一個事件即可
<template> <div class="ui-fas"> <el-input v-model="name" @input.native="filterIcons" suffix-icon="el-icon-search" placeholder="請輸入圖標名稱"></el-input> <ul class="fas-icon-list"> <li v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)"> <i class="fas" :class="['fa-' + item]" /> <span>{{item}}</span> </li> </ul> </div> </template> <script> import fontawesome from '@/extend/fontawesome/solid.js' export default { name: 'compIcons', data () { return { name: '', iconList: fontawesome } }, methods: { filterIcons () { if (this.name) { this.iconList = this.iconList.filter(item => item.includes(this.name)) } else { this.iconList = fontawesome } }, selectedIcon (name) { this.$emit('selected', name) }, reset () { this.name = '' this.iconList = fontawesome } } } </script>先把拿到的所有圖標name放到一個 solid.js 文件中,輸出為數組,在組件中引入,然后就是循環數組
iconList
,輸出i
標簽,Fontawesome 的使用方式是:<i></i>
。篩選功能利用數組的 filter 方法,
this.$emit('selected', name)
方式返回給父組件圖標名稱。以上樣式都是利用Element UI 的 Popover、Input 組件實現
<el-form-item label="圖標:" > <el-popover placement="left-start" width="540" trigger="click" @show="$refs.icons.reset()" popper-class="popper-class"> <ui-icons ref="icons" @selected="selectedIcon" /> <el-input slot="reference" placeholder="請輸入內容" readonly v-model="form.menu_icon" > <template slot="prepend"><i class="fas" :class="['fa-' + form.menu_icon]"></i></template> </el-input> </el-popover> </el-form-item>組件實現了,接下來就是引用,既可以直接到導入此組件引用,也可以掛載到全局進行使用,這里說說掛載到全局使用的方式,因為我的項目中所有的公共組件都是掛載到全局的方式使用。
在組件平級新建一個 index.js 文件
import IconsCompontent from './Icons.vue' const Icons = { install(Vue) { Vue.component('ui-icons', IconsCompontent); } } export default Icons;第4行為組件命名,此名稱決定了如何使用組件,這里是
ui-icons
,那么使用的時候就是:<ui-icons />接著在項目 components 根目錄新建 index.js,這里是所有組件的集合
最后一步是在 main.js 中注冊:
import CustomComponents from './components/index.js' Object.keys(CustomComponents).forEach(key => Vue.use(CustomComponents[key]))這樣就可以在項目中任意
.vue
文件中以<ui-icons />
方式使用組件了。后記
點擊圖標后要不要關閉圖標彈出層(Popover)呢?Popover 是需要鼠標點擊其他地方才會隱藏的,選擇一個圖標后就關閉 Popover 呢,我的做法是:
document.body.click()
。selectedIcon (name) { this.form.menu_icon = name // document.body.click() }關于Vue.js 中怎么實現一個圖標選擇組件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。