vue项目使用svg图片

(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、创建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、创建文件夹存放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组件

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue使用SVG矢量图,你可以按照以下步骤进行配置: 1. 首先,确保你的项目中有一个存放SVG图标的文件夹,比如`src/icons`。在这个文件夹中,你可以存放所有的SVG图标文件。 2. 在你的组件中,使用`<svg-icon>`组件来引用SVG图标。可以通过设置`icon-class`属性来指定SVG图标的名称,通过设置`className`属性来修改SVG图标的样式,比如大小和颜色。例如,`<svg-icon icon-class="tree" className="需要修改的样式" />`。 3. 配置`vue.config.js`文件,添加以下代码片段到`chainWebpack`方法中: ```javascript chainWebpack(config) { config.module .rule('svg') .exclude.add(resolve('src/icons')) // 注意svg的存储地址 .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) // 注意svg的存储地址 .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() } ``` 这段代码的作用是配置Webpack,使其能够正确地加载和处理SVG图标。其中,`exclude`和`include`方法用于指定SVG图标文件的存放地址,`svg-sprite-loader`用于加载和处理SVG图标文件,并通过`symbolId`选项设置图标的ID。 通过以上配置,你就可以在Vue项目使用SVG矢量图了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue中如何使用svg](https://blog.csdn.net/weixin_47909202/article/details/123015710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值