Home > Web Front-end > Vue.js > An article explaining in detail how to introduce SVG icons in Vue3 projects

An article explaining in detail how to introduce SVG icons in Vue3 projects

青灯夜游
Release: 2022-09-05 11:30:58
forward
3883 people have browsed it

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!

An article explaining in detail how to introduce SVG icons in Vue3 projects

Introduction

Through the previous period of learningExpress and Mysql

Here we try to build a backend system to consolidate the learning technology.

SVG icon

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

Alibaba Icon Library

An article explaining in detail how to introduce SVG icons in Vue3 projects

Enter the page, find Resource Management below My Project, and create the project

settings As follows

An article explaining in detail how to introduce SVG icons in Vue3 projects

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

An article explaining in detail how to introduce SVG icons in Vue3 projects

An article explaining in detail how to introduce SVG icons in Vue3 projects

##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, select

Symbol and Download it locally

An article explaining in detail how to introduce SVG icons in Vue3 projects

Put it in the

src\assets\svg directory

Unzip it and delete unnecessary Just leave the

iconfont.js file

An article explaining in detail how to introduce SVG icons in Vue3 projects

and then import it globally in

main.ts

import './assets/svg/iconfont.js'
Copy after login

Introduce svg-icon into the project

In the

src directory, create a directory for storing plug-insplugin

An article explaining in detail how to introduce SVG icons in Vue3 projects

// index.vue

<template>
  <svg>
    <use></use>
  </svg>
</template>

<script>
import { computed } from &#39;vue&#39;
const props = defineProps({
  iconName: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default: &#39;&#39;
  },
  color: {
    type: String,
    default: &#39;#409eff&#39;
  }
})
// 图标在 iconfont 中的名字
const iconClassName = computed(() => {
  return `#${props.iconName}`
})
// 给图标添加上类名
const svgClass = computed(() => {
  if (props.className) {
    return `svg-icon ${props.className}`
  }
  return &#39;svg-icon&#39;
})
</script>

<style>
.svg-icon {
  width: 1em;
  height: 1em;
  position: relative;
  fill: currentColor;
  vertical-align: -2px;
}
</style>
Copy after login
// index.ts

import { App } from 'vue'

import SvgIcon from './index.vue'

export function setupSvgIcon(app: App) {
  app.component('SvgIcon', SvgIcon)
}
Copy after login
Then register the component in

main.ts

import { setupSvgIcon } from './plugin/SvgIcon/index'
setupSvgIcon(app)
Copy after login
Use it in the page

<template>
  <div> 工作台页面 </div>

  <svg-icon></svg-icon>
</template>
Copy after login

An article explaining in detail how to introduce SVG icons in Vue3 projects

You can see that the

SVG icon is successfully displayed

Summary

Through

Express-Mysql-Vue3-TS-Pinia Make a backend system project

Introduce Alibaba The

SVG 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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template