index.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
  3. <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
  4. <use :xlink:href="iconName" />
  5. </svg>
  6. </template>
  7. <script>
  8. // +----------------------------------------------------------------------
  9. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  10. // +----------------------------------------------------------------------
  11. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  12. // +----------------------------------------------------------------------
  13. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  14. // +----------------------------------------------------------------------
  15. // | Author: CRMEB Team <admin@crmeb.com>
  16. // +----------------------------------------------------------------------
  17. // doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
  18. import { isExternal } from '@/utils/validate'
  19. export default {
  20. name: 'SvgIcon',
  21. props: {
  22. iconClass: {
  23. type: String,
  24. required: true
  25. },
  26. className: {
  27. type: String,
  28. default: ''
  29. }
  30. },
  31. computed: {
  32. isExternal() {
  33. return isExternal(this.iconClass)
  34. },
  35. iconName() {
  36. return `#icon-${this.iconClass}`
  37. },
  38. svgClass() {
  39. if (this.className) {
  40. return 'svg-icon ' + this.className
  41. } else {
  42. return 'svg-icon'
  43. }
  44. },
  45. styleExternalIcon() {
  46. return {
  47. mask: `url(${this.iconClass}) no-repeat 50% 50%`,
  48. '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
  49. }
  50. }
  51. }
  52. }
  53. </script>
  54. <style scoped>
  55. .svg-icon {
  56. width: 1em;
  57. height: 1em;
  58. vertical-align: -0.15em;
  59. fill: currentColor;
  60. overflow: hidden;
  61. }
  62. .svg-external-icon {
  63. background-color: currentColor;
  64. mask-size: cover!important;
  65. display: inline-block;
  66. }
  67. </style>