在 Vue 3 中使用阿里巴巴矢量图标库

在项目中基本会用到图标,比较常见的就是阿里图标库。这篇文章主要介绍如何在vue3中使用图标库。

下载并全局注册自定义图标库

  1. 手动下载阿里巴巴矢量图标库的字体文件:
    • 在阿里巴巴矢量图标库网站上选择您需要的图标,并将其添加到购物车。
    • 然后下载文件。
    • 解压下载的文件,将字体文件复制到您的 Vue 3 项目的合适目录例如 src/assets/fonts
      在这里插入图片描述
      在这里插入图片描述
  2. MyIcon.vue 组件中使用自定义图标:
<template>
  <span :class="['iconfont', `${name}`]"></span>
</template>

<script setup lang="ts">
import { defineProps } from 'vue';

defineProps({
  name: {
      type: String ,
      required: true,
    }
});
</script>

<style scoped>

</style>
  1. main.js文件引入
import { createApp } from 'vue';
import App from './App.vue';
import './assets/icons.css'; // 引入自定义图标样式文件
import MyIcon from '@/components/MyIcon/MyIcon.vue'; // 你自己的vue文件路径
const app = createApp(App);
app.component('MyIcon', MyIcon); // 使用
app.mount('#app');
  1. 使用例子
<template>
  <div>
    <my-icon name="iconsearch"></my-icon>
   <!-- 这里的iconsearch是图标名字 -->
  </div>
</template>
<script setup lang="ts"></script>
<style lang=""></style>

效果如下:
在这里插入图片描述

使用svg添加图标

  1. 安装插件vite-plugin-svg-icons
npm i vite-plugin-svg-icons;
//或者yarn
yarn add vite-plugin-svg-icons
// 或者pnpm
pnpm i vite-plugin-svg-icons
  1. 在配置文件中引用。我使用的vite构建的项目,所以就在vite.config.js文件操作就行
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

//1、 引入插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://vitejs.dev/config/
// 2、添加createSvgIconsPlugin
export default defineConfig({
  plugins: [vue(),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]',
    }),
  ],
resolve: { alias: { '@': path.resolve('./src') } },
})
  1. 在main.js文件引入
// 引入icon svg插件需要配置代码
import 'virtual:svg-icons-register'
  1. 添加svg文件
    一般在assets目录下放svgIcon目录,然后创建你所需要的svg文件,我用的是一个collapse文件。
    在这里插入图片描述
    放上svg代码就行了,比如:
<svg t="1684324017014" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1576"
    width="300" height="300">
    <path
        d="M631.808 838.784a32.704 32.704 0 0 1-32.704 32.704H32.64a32.704 32.704 0 0 1 0-65.408h566.464a32.704 32.704 0 0 1 32.704 32.704m0-217.6a32.64 32.64 0 0 1-32.704 32.64H32.64a32.704 32.704 0 0 1 0-65.344h566.464a32.704 32.704 0 0 1 32.704 32.704m187.84 0l138.944 138.944V482.24z m-187.84-217.6a32.704 32.704 0 0 1-32.704 32.704H32.64a32.64 32.64 0 0 1 0-65.344h566.464a32.704 32.704 0 0 1 32.704 32.64m371.968-30.144a32.704 32.704 0 0 1 20.224 30.144v435.776a32.704 32.704 0 0 1-55.808 23.104l-217.6-217.6a32.576 32.576 0 0 1 0-46.208l217.6-217.6a32.576 32.576 0 0 1 35.584-7.104M1024 186.24a32.704 32.704 0 0 1-32.704 32.704H32.64a32.704 32.704 0 0 1 0-65.344h958.656a32.64 32.64 0 0 1 32.704 32.64"
        p-id="1577"></path>
</svg>
  1. 使用
<template>
  <h1>我是图标</h1>
  <svg>
  // 这里的使用你引入svg的文件名字 #icon-是固定的name替换成文件名就行
    <use href="#icon-collapse" color="blue" style="width: 16px; height:16px"></use>
  </svg>
</template>

<script setup lang="ts">
import { defineProps } from 'vue';
</script>
<style scoped></style>

效果如下图所示:
在这里插入图片描述
当然我只介绍了我熟悉的方式。如有问题欢迎留言讨论。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Vue3 TS在线预览Word和Excel文件,需要使用第三方库。以下是一个基于Office Web Viewer的示例代码: 1. 首先安装依赖: ``` npm install --save office-ui-fabric-core@11.0.0 office-ui-fabric-react@7.0.0 office-ui-fabric-js@1.4.0 ``` 2. 引入相关的模块: ``` import { Fabric } from 'office-ui-fabric-react/lib/Fabric'; import { initializeIcons } from '@uifabric/icons'; import { IOUtils } from '@microsoft/office-js-helpers'; ``` 3. 初始化图标和Office Web Viewer: ``` initializeIcons(); const officeJS = new OfficeJS({ host: OfficeJS.HostType.WEB, platform: OfficeJS.PlatformType.PC, version: '16.00' }); ``` 4. 在Vue组件的template添加一个用于渲染预览的div: ``` <template> <div> <div ref="previewContainer"></div> </div> </template> ``` 5. 在Vue组件的script使用以下代码加载Word或Excel文件: ``` import { Fabric } from 'office-ui-fabric-react/lib/Fabric'; import { initializeIcons } from '@uifabric/icons'; import { IOUtils } from '@microsoft/office-js-helpers'; export default { name: 'FilePreview', props: { fileUrl: String, fileType: { type: String, default: 'Word' } }, mounted() { this.loadPreview(); }, methods: { async loadPreview() { const previewContainer = this.$refs.previewContainer; const fabric = new Fabric(); fabric.initialize(); const result = await IOUtils.xhr({ url: this.fileUrl, responseType: 'arraybuffer' }); const fileArray = this.arrayBufferToBase64(result.response); const fileBase64 = `data:${this.getFileMimeType()};base64,${fileArray}`; const viewer = new OfficeViewer({ appEl: previewContainer, fileType: this.fileType, fileUrl: fileBase64, officeJS: officeJS }); viewer.render(); }, getFileMimeType() { switch (this.fileType) { case 'Excel': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; case 'Word': default: return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; } }, arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); } } }; ``` 这样就实现Vue3 TypeScript在线预览Word和Excel文件。请注意,Office Web Viewer需要联网才能工作,因为它使用了云端的Office服务。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值