当前位置:   article > 正文

iconify的使用_@iconify

@iconify

如何在vue中使用iconify组件

 参考链接:图标 | FastCrud

  •  安装依赖
  1. //package.json
  2. {
  3. "dependencies": {
  4. "@iconify/iconify": "^3.0.1",
  5. }
  6. "devDependencies": {
  7. "unplugin-icons": "^0.15.1",
  8. }
  9. }
  • 配置Vite插件 
  1. // vite.config.js
  2. import Icons from 'unplugin-icons/vite'
  3. return {
  4. plugins: [
  5. vue(),
  6. Icons({compiler: 'vue3', autoInstall: true}) // 主要配置它
  7. ]
  8. }
  •  创建icon组件,并注册成全局组件
  1. // icon.vue
  2. <template>
  3. <span ref="iconifyRef" :class="[$attrs.class, 'my-iconify']" :style="getIconifyStyle"></span>
  4. </template>
  5. <script setup lang="ts">
  6. import {computed, nextTick, onMounted, ref, unref, watch} from "vue";
  7. import {propTypes} from '@/utils/propTypes'
  8. import Iconify from "@iconify/iconify/dist/iconify";
  9. const props = defineProps({
  10. icon: propTypes.string,
  11. color: propTypes.string,
  12. size: propTypes.number.def(0.2)
  13. })
  14. const iconifyRef = ref<ElRef>(null)
  15. const getIconifyStyle = computed(() => {
  16. const {color, size} = props
  17. return {
  18. fontSize: `${size}rem`,
  19. display: "inline-flex",
  20. color
  21. }
  22. })
  23. const updateIcons = async () => {
  24. if (!props.icon) return;
  25. const el = unref(iconifyRef);
  26. if (!el) return;
  27. await nextTick();
  28. const svg = Iconify.renderSVG(props.icon, {})
  29. if (svg) {
  30. el.textContent = '';
  31. el.appendChild(svg);
  32. } else {
  33. const span = document.createElement('span');
  34. span.className = 'iconify';
  35. span.dataset.icon = props.icon;
  36. el.textContent = '';
  37. el.appendChild(span);
  38. }
  39. }
  40. onMounted(() => updateIcons())
  41. </script>
  1. import type {App} from 'vue'
  2. import {Icon} from './icon'
  3. // 全局组件
  4. export const setupGlobCom = (app: App<Element>): void => {
  5. app.component('Icon', Icon)
  6. }

  •  选择图标

                官网地址:Icon Sets • Iconify

  • 使用
        <Icon icon="ion:skull" color="red" size="0.2"/>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/606141
推荐阅读
相关标签
  

闽ICP备14008679号