SoFunction
Updated on 2024-11-07

Analysis of vue cli3 wrapping Svgicon component correct posture (recommended)

vue cli3 Hands-On Teaching to Package Svgicon Component

Step 1: Create a new folder for svg files under src file.

在这里插入图片描述

Step 2: Create a new Svg component in the components file.

This is a folder dedicated to storing svg files that will be used in the project, such as ...

在这里插入图片描述

svg component source code

<template>
 <svg :class="svgClass" aria-hidden="true">
  <use :xlink:href="iconName"></use>
 </svg>
</template>

<script type="text/ecmascript-6">
/* The icon component implements auto-introduction ... /... /Icons/svg.
*After that, we're going to use webpack.
Many of you may be unfamiliar with webpack.
The straightforward explanation is (". /test", false, /. /test", false, /.)
This line of code will go to the test folder (without subdirectories)
The following looks for any file whose name ends in . This line of code will look for all files in the test folder (without the subdirectories) whose names end in .
In plain English, this means that we can introduce the appropriate file module */ by regular matching.
const requireAll = requireContext => ().map(requireContext)
const req = ('../../Icons/svg', false, /\.svg$/)
requireAll(req)
export default {
 name: 'Svgicon',
 props: {
  iconClass: {
   type: String,
   required: true
  },
  className: {
   type: String
  }
 },
 computed: {
  iconName () {
   return `#icon-${}`
  },
  svgClass () {
   if () {
    return 'svg-icon ' + 
   } else {
    return 'svg-icon'
   }
  }
 }
}
</script>

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

Step 3: Configuration

  • Required loader
  • npm i svg-sprite-loader -D
chainWebpack: config => {
  const svgRule = ('svg')
  ()
  svgRule
   .use('svg-sprite-loader')
   .loader('svg-sprite-loader')
   .options({
    symbolId: 'icon-[name]'
   })
}

 The above is configured

Note: Unregistered components can be used by registering them globally in the population file ().

To this point this article on the analysis of vue cli3 package Svgicon component correct posture (recommended) of the article is introduced to this, more related to vue cli3 Svgicon component content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!