element svg-icon使用

清疚 2022-12-24 00:50 232阅读 0赞

注册svg-icon组件,并引入svg

  1. import Vue from 'vue'
  2. import SvgIcon from '@/components/SvgIcon'// svg component
  3. // register globally
  4. Vue.component('svg-icon', SvgIcon)
  5. const req = require.context('./svg', false, /\.svg$/)
  6. const requireAll = requireContext => requireContext.keys().map(requireContext)
  7. requireAll(req)
  8. <template>
  9. <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
  10. <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
  11. <use :xlink:href="iconName" />
  12. </svg>
  13. </template>
  14. <script>
  15. // doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
  16. import { isExternal } from '@/utils/validate'
  17. export default {
  18. name: 'SvgIcon',
  19. props: {
  20. iconClass: {
  21. type: String,
  22. required: true
  23. },
  24. className: {
  25. type: String,
  26. default: ''
  27. }
  28. },
  29. computed: {
  30. isExternal() {
  31. return isExternal(this.iconClass)
  32. },
  33. iconName() {
  34. return `#icon-${ this.iconClass}`
  35. },
  36. svgClass() {
  37. if (this.className) {
  38. return 'svg-icon ' + this.className
  39. } else {
  40. return 'svg-icon'
  41. }
  42. },
  43. styleExternalIcon() {
  44. return {
  45. mask: `url(${ this.iconClass}) no-repeat 50% 50%`,
  46. '-webkit-mask': `url(${ this.iconClass}) no-repeat 50% 50%`
  47. }
  48. }
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. .svg-icon {
  54. width: 1em;
  55. height: 1em;
  56. vertical-align: -0.15em;
  57. fill: currentColor;
  58. overflow: hidden;
  59. }
  60. .svg-external-icon {
  61. background-color: currentColor;
  62. mask-size: cover!important;
  63. display: inline-block;
  64. }
  65. </style>

mian.js中引入

  1. import './icons' // icon

使用

  1. <svg-icon icon-class="bug" />

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Element ui使用相关

    v-if 切换 table,导致表格数据显示错乱 原因:使用v-if切换标签时,多个相同的标签被渲染,如果不添加key来区分则会出现数据冲突导致table渲染出错 解决