Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@aidol/svg-icon

yisibell42MITdeprecated2.1.4

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

A Vue SVG Symbol icon component for svg-sprite-loader, Easy to custom SVG icon 's color and size!!!

svg-symbol-icon, vue, svg, icon, component, aidol, svg-sprite-loader, webpack, nuxt

readme


WARNING: This Package is no longer maintained, Please use vue-symbol-icon instead.

@aidol/svg-icon

A Vue SVG Symbol icon component for svg-sprite-loader, Easy to custom SVG icon 's color and size!!!

TIPS: @aidol/svg-icon needs to be used in conjunction with svg-sprite-loader . So, please pre-install svg-sprite-loader and config it.

Features

  • Ability to manipulate SVG icons. e.g. using font-size and color.
  • Support Iconfont svg icons.

Installation

# pnpm
$ pnpm add @aidol/svg-icon

# yarn
$ yarn add @aidol/svg-icon

# npm
$ npm i @aidol/svg-icon

Usage

demo.vue

<template>
  <svg-icon icon-class="svg-symbol-id" font-size="36px" color="red" />
</template>

Properties

Prop name Default value Required Description Type
icon-class undefined true SVG Symbol Id which is SVG filename in the SVG folder. string
className undefined false Add Extra class name to SVG Element string
color undefined false Define SVG color string
fontSize undefined false Define SVG size string

How to config svg-sprite-loader ?

In Vue CLI

  1. First, you need config webpack with chainWebpack:
// vue.config.js
const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  // ...
  chainWebpack(config) {

    // Change the configuration of url-loader so that it does not process svg files used as icons in the specified folder
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end();

    // Add svg-sprite-loader to process svg files in the specified folder
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      })
      .end();
  }
}
  1. Then, place your .svg icon files in the src/icons/svg folder.

  2. Defines the entry point for batch importing .svg modules:

// src/icons/index.js

import Vue from 'vue'
import SvgIcon from '@aidol/svg-icon' // svg component

// 1. register globally
Vue.component('svg-icon', SvgIcon) 

// 2. require svg files
const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().forEach(requireContext)
requireAll(req)
  1. Finally, these .svg files are centrally imported in the project entry file main.js.
import Vue from 'vue'

import '@/icons'

new Vue({
  // ...
})

In Nuxt2

  1. First, config webpack in the nuxt.config.js:
// nuxt.config.js

export default {
  // ...
  // Build Configuration (https://go.nuxtjs.dev/config-build)
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, { isClient }) {
      if (isClient) {
        const svgRule = config.module.rules.find((rule) =>
          rule.test.test('.svg')
        )
        svgRule.exclude = [resolve('assets/icons/svg')]

        // Includes /assets/icons/svg for svg-sprite-loader
        config.module.rules.push({
          test: /\.svg$/,
          include: [resolve('assets/icons/svg')],
          loader: 'svg-sprite-loader',
          options: {
            symbolId: 'icon-[name]',
          },
        })
      }
    },
  }
  // ...
}
  1. Centralize your *.svg icon files in the ~/assets/icons/svg folder.

  2. Create a new ~/plugins/svg-icon.js file and write in it:

import Vue from 'vue'
import SvgIcon from '@aidol/svg-icon' // svg component

// 1. register globally
Vue.component('svg-icon', SvgIcon) 

// 2. require svg files
const req = require.context('~/assets/icons/svg', false, /\.svg$/)
const requireAll = (requireContext) => requireContext.keys().forEach(requireContext)
requireAll(req)
  1. Configure the svg-icon plugin to nuxt.config.js:
export default {
  // ...

  plugins: [
    // ...
    { src: '~/plugins/svg-icon', mode: 'client' }
  ]

  // ...
}

CHANGE LOG

CHANGE LOG.

changelog

CHANGE LOG

v2.1.4

🏡 Chore

  • docs: Warning tips (1718e50)

❤️ Contributors

v2.1.3

🏡 Chore

  • docs: Update readme (b78d203)

❤️ Contributors

v2.1.2

🏡 Chore

  • docs: Update readme to en (37d6f39)

❤️ Contributors

v2.1.1

🏡 Chore

  • docs: Update readme (271beb1)

❤️ Contributors

v2.1.0

🚀 Enhancements

  • Upgrade vue cli to v5 and support custom svg color and size (71b143b)

📖 Documentation

  • Update docs (ba3b444)

🏡 Chore

  • docs: Update readme (69769b5)

❤️ Contributors

  • 2022-02-28(version 1.3.0)

  • docs 更新文档。

  • 2020-11-06 (version 1.2.0)

  • Upgrade<use /> 标签的 xlink:href 属性改为 href,使其兼容 svg 2

  • 2020-08-26 (version 1.1.4)

  • BugFixs 去除图标颜色可自定义,原因是无法兼容多色图标。

  • 2020-08-26 (version 1.1.3)

  • Upgrade 优化图标颜色改变失败问题。