- 原文地址:Making SVG Icon Component in Vue
- 原文做者:Achhunna Mali
- 译文出自:掘金翻译计划
- 本文永久连接:github.com/xitu/gold-m…
- 译者:zoomdong
- 校对者:Xu Jianxiang
在考虑了将矢量图标从图标字体迁移到内联 SVG 的缘由以后,我在 Vue.js 中找到了一个用 SVG 替换图标字体的解决方案,同时仍能保持使用图标字体的灵活性和易用性——可以使用 CSS 轻松改变图标的大小、颜色以及其它属性。css
一种流行的方法是使用 v-html
指令和 npm 模块 html-loader
来将 SVG 导入到咱们的 Vue 模板中,并在 Vue 的生命周期函数 mounted()
中修改渲染的 <svg>
元素。CSS 样式能够直接应用到 <svg>
元素或者是其父元素上,而且这些可以组成一个可复用的组件。html
让咱们建立 Svg-icon.vue
组件文件,并在里面接收三个 prop 变量。前端
icon
是一个字符串类型的 prop 变量用来传递 .svg
文件名的导入hasFill
是一个布尔类型的 prop 变量来告诉组件 fill
属性是否用于更改 <svg>
元素的颜色,默认值为 false 即不使用 fill
属性growByHeight
是一个布尔类型的 prop 变量来决定 height
或 width
是否相对于 font-size
进行缩放,默认值为 true 即便用 height
<script>
function recursivelyRemoveFill(el) {
if (!el) {
return;
}
el.removeAttribute('fill');
[].forEach.call(el.children, child => {
recursivelyRemoveFill(child);
});
}
export default {
name: 'svg-icon',
props: {
icon: {
type: String,
default: null
},
hasFill: {
type: Boolean,
default: false
},
growByHeight: {
type: Boolean,
default: true
},
},
mounted() {
if (this.$el.firstElementChild.nodeName === 'svg') {
const svgElement = this.$el.firstElementChild;
const widthToHeight = (svgElement.clientWidth / svgElement.clientHeight).toFixed(2);
if (this.hasFill) {
// recursively remove all fill attribute of element and its nested children
recursivelyRemoveFill(svgElement);
}
// set width and height relative to font size
// if growByHeight is true, height set to 1em else width set to 1em and remaining is calculated based on widthToHeight ratio
if (this.growByHeight) {
svgElement.setAttribute('height', '1em');
svgElement.setAttribute('width', `${widthToHeight}em`);
} else {
svgElement.setAttribute('width', '1em');
svgElement.setAttribute('height', `${1 / widthToHeight}em`);
}
svgElement.classList.add('svg-class');
}
}
}
</script>
<template>
<div v-html="require(`html-loader!../assets/svg/${icon}.svg`)" class="svg-container"></div>
</template>
<style lang="scss" scoped>
.svg-container {
display: inline-flex;
}
.svg-class {
vertical-align: middle;
}
</style>
复制代码
咱们将 .svg
图标文件经过 require()
传递给 html-loader
方法,该方法会将文件字符串化,而且经过 v-html
指令将其渲染为 <svg>
元素。vue
全部对 <svg>
元素修改的地方都在 mounted()
生命周期方法里面。node
growByHeight
定义的 <svg>
元素的 height
或 width
属性设置为 1em
(font-size
的一倍)并对另外一个元素使用 widthToHeight
。因为并不是全部的 SVG 都是正方形的,所以咱们从渲染的元素计算 withToHeight
比率,以便 SVG 在父元素的 font-size
属性大小改变的时候按比例缩放到其原始尺寸。<svg>
元素的 fill
属性,咱们须要覆盖掉 SVG 文件内部附带的 fill
属性。当 hasFill
值为 true 的时候,咱们从 <svg>
元素及其子元素中递归地删除 fill 属性。而后使用 CSS 选择器将 fill 值添加到其父元素或 <svg>
元素就能够了。class
等其它 DOM 属性,这些属性可用于设置组件中的范围样式让咱们使用 Font Awesome 和咱们的 Svg-icon
组件中的图标字体建立一个微笑图标。android
<template>
<i class="fas fa-smile smile-icon"></i>
</template>
<style lang="scss" scoped>
.smile-icon {
font-size: 24px;
color: #aaa;
&:hover {
color: #666;
}
}
</style>
复制代码
.smile-icon
类的 CSS 选择器以及伪类选择器 :hover
来设置图标的 font-size
和 color
属性。ios
<script>
import SvgIcon from './components/Svg-icon';
export default {
name: 'my-component',
components: {
'svg-icon': SvgIcon,
},
}
</script>
<template>
<div class="smile-icon">
<svg-icon icon="smile-solid" :hasFill="true"></svg-icon>
</div>
</template>
<style lang="scss" scoped>
.smile-icon {
font-size: 24px;
fill: #aaa;
&:hover {
fill: #666;
}
}
</style>
复制代码
上面的实现和图标字体方法相同,除了 .smile-icon
类在父元素中,在 Svg-icon
组件中,color
属性被替换为 fill
。咱们还必须确保 smile-solid.svg
文件位于 Svg-icon
组件的 require()
方法指定的路径(./assets/svg/
)中。git
这是由 v-html
的输出渲染的 HTML。注意:会删除掉全部的 fill
属性,并将 height
和 width
属性添加到 <svg>
中。github
<div class="smile-icon">
<svg height="1em" width="1em" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="smile" class="svg-inline--fa fa-smile fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512">
<path d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 168c17.7 0 32 14.3 32 32s-14.3 32-32 32-32-14.3-32-32 14.3-32 32-32zm-160 0c17.7 0 32 14.3 32 32s-14.3 32-32 32-32-14.3-32-32 14.3-32 32-32zm194.8 170.2C334.3 380.4 292.5 400 248 400s-86.3-19.6-114.8-53.8c-13.6-16.3 11-36.7 24.6-20.5 22.4 26.9 55.2 42.2 90.2 42.2s67.8-15.4 90.2-42.2c13.4-16.2 38.1 4.2 24.6 20.5z">
</path>
</svg>
</div>
复制代码
因为 SVG 被认为是将来的发展方向,所以最好是在保留图标字体的易用性的基础上,逐步放弃使用图标字体。Svg-icon
组件是一个例子,告诉了咱们如何使用可用的库来抽离出 <svg>
元素中的混乱部分,同时模仿使用图标字体的好处!npm
若是发现译文存在错误或其余须要改进的地方,欢迎到 掘金翻译计划 对译文进行修改并 PR,也可得到相应奖励积分。文章开头的 本文永久连接 即为本文在 GitHub 上的 MarkDown 连接。
掘金翻译计划 是一个翻译优质互联网技术文章的社区,文章来源为 掘金 上的英文分享文章。内容覆盖 Android、iOS、前端、后端、区块链、产品、设计、人工智能等领域,想要查看更多优质译文请持续关注 掘金翻译计划、官方微博、知乎专栏。