vue中寫svg組件svg圖片加載不出來
結構
首先要安裝3個插件:svg-sprite-loader,svgo,svgo-loader
npm install svg-sprite-loader -S
npm install svgo -S
npm install svgo-loader -S
package.json
src/icons/index.js
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg組件
// register globally
Vue.component('svg-icon', SvgIcon)
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)
src/components/SvgIcon/index.vue
<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName"></use>
</svg>
</template>
<script>
export default {
name: 'svg-icon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String
}
},
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: 1.2em;
height: 1.2em;
vertical-align: -0.18em;
fill: currentColor;
overflow: hidden;
}
</style>
main.js文章來源:http://www.zghlxwxcb.cn/news/detail-612794.html
import '@/icons' // icon
vue.config.js文章來源地址http://www.zghlxwxcb.cn/news/detail-612794.html
const path = require('path')
module.exports = {
chainWebpack: config => {
// 給svg規(guī)則增加?個排除選項
config.module
.rule('svg')
.exclude.add(path.resolve(__dirname, './src/icons'))
// 新增icons規(guī)則,設置svg-sprite-loader處理icons?錄中的svg
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(path.resolve(__dirname, './src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({ symbolId: 'icon-[name]' })
.end()
.use('svgo-loader')
.loader('svgo-loader')
// config.resolve.alias.set('@img', path.resolve(__dirname, 'src/assets/img/'))
},
}
到了這里,關于vue中寫svg組件svg圖片加載不出來的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!