// src/common/Icon-svg.vue <template> <svg class="svg-icon" aria-hidden="true"> <use :xlink:href="iconName"></use> </svg> </template> <script> export default { name: 'icon-svg', props: { iconClass: { type: String, required: true } }, computed: { iconName() { return `#icon-${this.iconClass}` } } } </script> <style> .svg-icon { width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; } </style>
// main.js全局引入iconfont.js和自定義的IconSvg組件 import "./assets/icon/iconfont.js"; import IconSvg from './common/Icon-svg.vue'; Vue.component('icon-svg', IconSvg);
直接使用icon-class
填iconfont上的後綴名,如圖標名爲icon-download
,則填download
vue
<icon-svg icon-class="download" />