vue頁面如何引入svg圖標


在做圖標展示時,一般使用fontawesome圖標庫,只用簡單並且只需要下載並引入即可。npm install font-awesome --save

但是發現身邊也有人使用阿里巴巴的incofont,下載選擇svg文件引入,具體封裝和配置方法如下示:

以下操作是參考了已有框架的代碼進行整理

 

1、在src/components下創建文件夾,命名為SvgIcon,並再SvgIcon文件夾下,新增目錄index.vue文件:  

<template>
    <svg :class="svgClass" aria-hidden="true" v-on="$listeners">
        <use :xlink:href="iconName" />
    </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'
                }
            },
            styleExternalIcon() {
                return {
                    mask: `url(${this.iconClass}) no-repeat 50% 50%`,
                    '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
                }
            }
        }
    }
</script>

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

    .svg-external-icon {
        background-color: currentColor;
        mask-size: cover!important;
        display: inline-block;
    }
</style>

 

2、在src目錄下,新增文件夾,命名為icons,並再icons文件夾下,新增目錄index.js文件和svg文件夾,其中svg文件夾里面存放的是svg文件。

  以下是src/icons/index.js文件的內容:

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon' // svg組件
// 注冊為全局組件
Vue.component('svg-icon', SvgIcon)
const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

 

3、在vue.config.js文件中,配置svg文件,其中chainWebpack里面的內容為svg的配置

module.exports = {
    devServer: {
        port: 8000
    },
    configureWebpack: {
        name: projectName,
        resolve: {
            alias: {
                '@': resolve('src'),
                'views': resolve('src/views')
            }
        }
    },
    chainWebpack(config) {
        config.module
            .rule('svg')
            .exclude.add(resolve('src/icons'))
            .end()
        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()
    }
}

 

4、在main.js直接引入inco文件夾

// 引入全局inco
import '@/icons'

  

5、在頁面直接使用組件svg-icon,其中incoClass命名等於svg文件的名稱

<svg-icon iconClass="example"/>
<svg-icon iconClass="message"/>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM