How to introduce SVG icons into Vue3 projects? The following article will introduce to you how to introduce iconfont icons into Vue3 projects. I hope it will be helpful to you!
Through the previous period of learningExpress and Mysql
Here we try to build a backend system to consolidate the learning technology.
Since it is a page, it must be inseparable from some iconsicon, so be sure to go to the most complete Alibaba icon Library to find
Here we explain how to put the contents of Alibaba Icon Library on our page
Enter the page, find Resource Management below My Project, and create the project
settings As follows
After creating the project, we enterAli’s material library to find some icons we need later,
and add them Go to Shopping Cart,
Add the icon in Shopping Cart to the project
##There will be an online icon link address before, let us introduce it. But it is not found now. It should be downloaded locally and then used = =Then we can only select the icon in the project, selectSymbol and Download it locally
Put it in thesrc\assets\svg directory
Unzip it and delete unnecessary Just leave theiconfont.js file
and then import it globally inmain.ts
import './assets/svg/iconfont.js'
src directory, create a directory for storing plug-insplugin
// index.vue <template> <svg> <use></use> </svg> </template> <script> import { computed } from 'vue' const props = defineProps({ iconName: { type: String, required: true }, className: { type: String, default: '' }, color: { type: String, default: '#409eff' } }) // 图标在 iconfont 中的名字 const iconClassName = computed(() => { return `#${props.iconName}` }) // 给图标添加上类名 const svgClass = computed(() => { if (props.className) { return `svg-icon ${props.className}` } return 'svg-icon' }) </script> <style> .svg-icon { width: 1em; height: 1em; position: relative; fill: currentColor; vertical-align: -2px; } </style>
// index.ts import { App } from 'vue' import SvgIcon from './index.vue' export function setupSvgIcon(app: App) { app.component('SvgIcon', SvgIcon) }
main.ts
import { setupSvgIcon } from './plugin/SvgIcon/index' setupSvgIcon(app)
<template> <div> 工作台页面 </div> <svg-icon></svg-icon> </template>
SVG icon is successfully displayed
Express-Mysql-Vue3-TS-Pinia Make a backend system project
Introduce Alibaba TheSVG icon into the project.
[Related recommendations:vuejs introductory tutorial]
The above is the detailed content of An article explaining in detail how to introduce SVG icons in Vue3 projects. For more information, please follow other related articles on the PHP Chinese website!