Steps for packaging and configuring SVG components in Vue projects

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getting the project, I found an interesting thing that the title icon is made with svg. This article thoroughly explains how to use it.

1. Create a vue project (use cli to build scaffolding, this test project is configured with vue cli4)

2. Create a custom component

insert image description here

The specific code is as follows:

<template>
  <svg :class="svgClass" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="iconName" rel="external nofollow" />
  </svg>
</template>

<script>
export default {
  name: "SvgIcon",
  props: {
    iconClass: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: "",
    },
  },
  computed: {
    iconName() {
      return `#icon-${this.iconClass}`;
    },
    svgClass() {
      if (this.className) {
        return "svg-icon " + this.className;
      } else {
        return "svg-icon";
      }
    },
  },
};
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

3. Create icons in the root directory, create a new index.js (which will be globally imported later), and create a new svg directory to store svg images (as for how to download svg, you can download it from Alibaba's iconfont, just search Baidu)

insert image description here

The specific code of index.js is as follows:

import Vue from 'vue'
import SvgIcon from '@/components/svgIcon' // svg component // register globally
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

4. Globally import main.js for introduction

insert image description here

5. At this time, the project also needs to configure vue.config.js (otherwise it will not be displayed)

const path = require('path')
module.exports = {
    chainWebpack: config => {
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule
            .test(/\.svg$/)
            .include.add(path.resolve(__dirname, './src/icons')).end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
        const fileRule = config.module.rule('file')
        fileRule.uses.clear()
        fileRule
            .test(/\.svg$/)
            .exclude.add(path.resolve(__dirname, './src/icons'))
            .end()
            .use('file-loader')
            .loader('file-loader')
    }
}

That's it;

6. Use components in the project

Here I use functional components to introduce, which can also be introduced through normal component usage methods

<script>
export default {
  functional: true,
  props: {
    level:
      type: Number,
      required: true,
    },
  },
  render: function (h, context) {
    console.log(context);
    let vnodes = [];
    let { level } = context.props;
    // vnodes.push(<i class="el-icon-edit" style="width:19px"></i>);
    vnodes.push(<svg-icon icon-class="date"></svg-icon>);
    vnodes.push(<span class="span">{level}</span>);
    return vnodes;
  },
};
</script>
<style scoped>
.span {
  font-size: 50px;
}
</style>

Note: The value of icon-class is directly the file name of svg.

This concludes this article about the steps for encapsulating and configuring SVG components in Vue projects. For more information about encapsulating and configuring Vue SVG components, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The whole process record of vue3 recursive component encapsulation
  • Vue pop-up box component encapsulation example code
  • Vue implements image preview component encapsulation and use
  • Implementation of Vue scope slots from component encapsulation
  • Implementation of regionalization and component encapsulation of Vuejs pages
  • How much do you know about Vue's all-select component encapsulation?

<<:  Does the website's text still need to be designed?

>>:  Display flex arrangement in CSS (layout tool)

Blog    

Recommend

A complete explanation of MySQL high availability architecture: MHA architecture

Table of contents 1. Introduction 2. Composition ...

Use VSCode's Remote-SSH to connect to Linux for remote development

Install Remote-SSH and configure it First open yo...

Detailed tutorial on installing Protobuf 3 on Ubuntu

When to install If you use the protoc command and...

Detailed explanation of pid and socket in MySQL

Table of contents 1. Introduction to pid-file 2.S...

Detailed tutorial on installing nvidia driver + CUDA + cuDNN in Ubuntu 16.04

Preparation 1. Check whether the GPU supports CUD...

Docker solution for logging in without root privileges

When you use the docker command for the first tim...

Summary of all HTML interview questions

1. The role of doctype, the difference between st...

Detailed description of ffmpeg Chinese parameters

FFMPEG 3.4.1 version parameter details Usage: ffm...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

uniapp realizes the recording upload function

Table of contents uni-app Introduction HTML part ...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

Detailed installation steps for MySQL 8.0.11

This article shares the installation steps of MyS...

Solutions to Files/Folders That Cannot Be Deleted in Linux

Preface Recently our server was attacked by hacke...

Learn SQL query execution order from scratch

The SQL query statement execution order is as fol...