午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Vue中引入svg圖標的兩種方式

瀏覽:7日期:2022-10-10 16:18:14

Vue中引入svg圖標的方式

Vue中引入svg圖標的方式一

安裝

yarn add svg-sprite-loader --dev

svg組件

Vue中引入svg圖標的兩種方式

index.vue

<!-- svg組件 --><template> <svg : aria-hidden='true'> <use :xlink:href='http://www.leifengta.com.cn/bcjs/iconName' /> </svg></template><script>export default { name: ’SvgIcon’, props: { // svg 的名稱 svgName: { type: String, required: true } }, computed: { iconName () { return `#icon-${this.svgName}` }, svgClass () { if (this.svgName) { return ’svg-icon’ + this.svgName } else { return ’svg-icon’ } } }}</script><style lang='less' scoped>.svg-icon { width: 100px; height: 100px; vertical-align: -0.15em; fill: currentColor; overflow: hidden;}</style>

注冊到全局

Vue中引入svg圖標的兩種方式

index.js

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

vue.config.js

module.exports = {chainWebpack: config => { config.module .rule(’svg’) .exclude.add(resolve(’src/assets/icons’)) .end() config.module .rule(’icons’) .test(/.svg$/) .include.add(resolve(’src/assets/icons’)) .end() .use(’svg-sprite-loader’) .loader(’svg-sprite-loader’) .options({ symbolId: ’icon-[name]’ }) .end()} }

頁面中使用

<!-- svg-name為svg名 --><svg-icon svg-name='ic_home_news' />Vue中引入svg圖標的方式二

npm install svg-sprite-loader --save-dev

vue.config.js中添加如下代碼

const path = require(’path’);function resolve(dir) { // __dirname項目根目錄的絕對路徑 return path.join(__dirname, dir);}module.exports = { chainWebpack: config => { const svgRule = config.module.rule(’svg’); // 清除已有的所有l(wèi)oader // 如果你不這樣做,接下來的loader會附加在該規(guī)則現(xiàn)有的loader之后 svgRule.uses.clear(); svgRule .test(/.svg$/) .include.add(path.resolve(__dirname, ’./src/icons/svg’)) .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/svg’)) .end() .use(’file-loader’) .loader(’file-loader’); },}

建立如下的文件目錄

Vue中引入svg圖標的兩種方式

SvgIcon.vue代碼

<template> <svg : xmlns='http://www.w3.org/2000/svg'> <use :xlink:href='http://www.leifengta.com.cn/bcjs/iconName' xmlns:xlink='http://www.w3.org/1999/xlink' /> </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>

svg文件夾下放svg圖標

index.js代碼

import Vue from ’vue’;import SvgIcon from ’@/components/SvgIcon’; // svg組件// register globallyVue.component(’svg-icon’, SvgIcon);const req = require.context(’./svg’, false, /.svg$/);const requireAll = requireContext => requireContext.keys().map(requireContext);requireAll(req);

最后在main.js中引入

import ’./icons’;

在頁面中使用svg

icon-class是svg圖標名 class-name是你要自定義的class類名

<svg-icon icon- class-name='icon'></svg-icon>總結(jié)

到此這篇關(guān)于Vue中引入svg圖標的兩種方式的文章就介紹到這了,更多相關(guān)Vue引入svg圖標內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 男人操女人的视频网站 | 黄色www.| 婷婷在线影院 | 毛片在线网址 | 久久精品综合 | 成人在线免费观看视频 | 亚洲国产精品影视 | 天堂一级片| 超碰成人av| 天堂视频网 | 天堂av一区二区三区 | 久久99国产综合精品免费 | 四虎国产成人永久精品免费 | 欧美亚洲国产一区二区三区 | 超碰少妇| 欧美激情中文字幕 | 国产一区精品在线观看 | www色婷婷 | 国产玖玖 | 日本激情久久 | 国产午夜激情 | 小视频在线免费观看 | 91精品国产一区二区三区 | 激情久久久久久久 | 极品少妇av | 欧美jizz19性欧美 | 国产成人福利在线 | 操女人逼视频 | 杨钰莹一级淫片aaaaaa播放 | 中文日韩在线观看 | 久久全国免费视频 | 久久9精品 | 亚洲第九页 | 日韩成人精品在线 | 国产精品久久一区 | 精品国产乱码久久久久久久 | 都市激情自拍偷拍 | 岛国裸体写真hd在线 | 中出av在线 | 精品少妇一区二区三区免费观看 | 午夜69成人做爰视频 |