vue項(xiàng)目中進(jìn)行svg組件封裝及配置方法步驟
最近剛?cè)胄碌墓?,拿到?xiàng)目之后,發(fā)現(xiàn)一個(gè)有趣的事情就是標(biāo)題的icon是用svg來弄的,這篇文章徹底弄清楚怎么使用
1.創(chuàng)建vue項(xiàng)目(通過cli來搭建腳手架,該測試項(xiàng)目是用vue cli4進(jìn)行配置的)
2.創(chuàng)建一個(gè)自定義組件
具體代碼如下:
<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.在根目錄創(chuàng)建icons,新建一個(gè)index.js(等會(huì)全局引入),和新建一個(gè)svg目錄,專門放svg圖片(至于怎么下載svg, 阿里的iconfont就可以下載,自行百度)
index.js的具體代碼如下:
import Vue from 'vue' import SvgIcon from '@/components/svgIcon'// svg組件 // register globally Vue.component('svg-icon', SvgIcon) const req = require.context('./svg', false, /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req)
4.全局引入main.js進(jìn)行引入
5.此時(shí)項(xiàng)目還需要進(jìn)行配置vue.config.js(不然無法顯示出來)
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') } }
這樣就完成了;
6.項(xiàng)目中進(jìn)行使用組件
這里我用的是函數(shù)式組件進(jìn)行引入,也可以通過正常的組件使用方法引入
<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>
注意:icon-class的值直接是svg的文件名。
到此這篇關(guān)于vue項(xiàng)目中進(jìn)行svg組件封裝及配置方法步驟的文章就介紹到這了,更多相關(guān)vue svg組件封裝配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue3&element-plus的暗黑模式實(shí)例詳解
實(shí)現(xiàn)暗黑主題的方式有很多種,也有很多成型的框架可以直接使用,下面這篇文章主要給大家介紹了關(guān)于基于vue3&element-plus的暗黑模式的相關(guān)資料,需要的朋友可以參考下2022-12-12關(guān)于vue-admin-template模板連接后端改造登錄功能
這篇文章主要介紹了關(guān)于vue-admin-template模板連接后端改造登錄功能,登陸方法根據(jù)賬號(hào)密碼查出用戶信息,根據(jù)用戶id與name生成token并返回,userinfo則是對token進(jìn)行獲取,在查出對應(yīng)值進(jìn)行返回,感興趣的朋友一起看看吧2022-05-05在vue中配置不同的代理同時(shí)訪問不同的后臺(tái)操作
這篇文章主要介紹了在vue中配置不同的代理同時(shí)訪問不同的后臺(tái)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09ant design Vue 純前端實(shí)現(xiàn)分頁問題
這篇文章主要介紹了ant design Vue 純前端實(shí)現(xiàn)分頁問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04elementUI select組件默認(rèn)選中效果實(shí)現(xiàn)的方法
這篇文章主要介紹了elementUI select組件默認(rèn)選中效果實(shí)現(xiàn)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue v-for直接循環(huán)數(shù)字實(shí)例
今天小編就為大家分享一篇vue v-for直接循環(huán)數(shù)字實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue.js實(shí)現(xiàn)一個(gè)漂亮、靈活、可復(fù)用的提示組件示例
這篇文章主要介紹了Vue.js實(shí)現(xiàn)一個(gè)漂亮、靈活、可復(fù)用的提示組件示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03