(svg-sprite-loader以及vue2-svg-icon的使用)
第一種方式:
1、安裝svg-sprite-loader
????????npm install svg-sprite-loader --save-dev
2、webpack 配置(build/webpack.base.conf.js)
????????
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
include: [resolve('src/icons')],
options: {
symbolId: 'icon-[name]'//去掉svg這個圖片加載不出來
}
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
//這個不處理svg圖片,因為我們獨立拆開讓svg-sprite-loader來處理了
exclude: [resolve('src/icons')],
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
3、創(chuàng)建svg的組件
????????
<template>
<svg :class="svgClass" aria-hidden="true">
<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'
}
}
}
}
</script>
<style scoped>
.svg-icon {
width: 10rem;
height: 10rem;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
?4、創(chuàng)建文件夾存放svg的圖標,同時注冊svg組件到vue里面(index.js)
單個使用如下:
import './assets/svg/target.svg';
<svg><use xlink:href="#target" /></svg>
嗯,就這樣短短一行。xlink:href 中傳入 svg ID 就好了,由于在上面的配置文件中我們直接使用文件名作為 symbol 的 ID,所以這里傳入的 ID 即為你想顯示的圖標的 svg 文件名,記得加上 #。
所有svg文件自動導入
index.js代碼如下,自動導入 src/icons/svg/ 下的 svg 文件。?
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)
六、在main.js中引入
import Vue from 'vue'
import App from './App'
import router from './router'
//引入整個icons,
import './icons'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
5、在vue中使用
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
<svg-icon icon-class="smile" size="10"></svg-icon>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
第二種方式:
1、安裝vue2-svg-icon
????????npm install vue2-svg-icon --save-dev
2、引入main.js并注冊組件
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//import './icons/index.js'
//引入vue2-svg-icon并且注冊組件
import Icon from 'vue2-svg-icon/Icon'
Vue.component('icon',Icon);
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
3、新建存放svg圖片的目錄(這個包默認是從assets/svg下面找svg圖片的,不要問我為什么
4、vue頁面使用svg組件文章來源:http://www.zghlxwxcb.cn/news/detail-409750.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-409750.html
到了這里,關(guān)于vue項目使用svg圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!