av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:6日期: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://m.4tl426be.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’); // 清除已有的所有loader // 如果你不這樣做,接下來的loader會附加在該規則現有的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://m.4tl426be.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>總結

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

標簽: Vue
相關文章:
主站蜘蛛池模板: 欧美一区视频在线 | 日本成人在线观看网站 | re久久 | 久久久精品一区二区三区四季av | 国产精品日韩一区二区 | 日韩av免费在线观看 | 日日操夜夜操天天操 | 蜜桃精品视频在线 | 国产中文 | 国产日韩精品在线 | 在线观看国产视频 | 欧美黄色大片在线观看 | 久久91精品国产一区二区 | 中文字幕一区二区视频 | 成人黄色a | 97精品国产97久久久久久免费 | 香蕉视频91 | 久久成人精品 | 久久久久国产精品 | 久久精品亚洲国产奇米99 | 欧美在线视频网站 | 一区二区三区视频在线观看 | 日韩插插 | 九七午夜剧场福利写真 | 欧美激情视频一区二区三区免费 | 日韩一级免费观看 | 欧美一区二区在线视频 | 四虎精品在线 | 一区免费| av国产精品毛片一区二区小说 | 成人一区在线观看 | 日本淫视频 | 欧美一级全黄 | 欧美视频一区二区三区 | 欧美日韩一区二区三区四区 | 国产精品久久久久久久久久久免费看 | 欧美a区 | 国产视频精品视频 | 中文字幕一区二区三 | 亚洲精品福利视频 | 日韩在线中文字幕 |