1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| <template>
| <!-- 如果是 Element UI 图标,保持不变 -->
| <i v-if="iconFileName.indexOf('el-icon-') === 0" :class="iconFileName" />
|
| <!-- 如果是 SVG 图标,进行改造 -->
| <svg v-else class="svg-icon" aria-hidden="true" v-on="$listeners" :style="svgStyle">
| <!--
| :xlink:href 动态绑定图标ID
| 注意:这里假设你的图标ID是 "icon-" + iconFileName
| -->
| <use :xlink:href="`#icon-${iconFileName}`" />
| </svg>
| </template>
|
| <script>
| import config from "@/assets/js/config";
| export default {
| name: 'SvgIcon',
| props: {
| iconFileName: {
| type: String,
| required: true
| },
| // 新增一个 color prop,用于接收颜色
| color: {
| type: String,
| // 默认值设为 'inherit',它会继承父元素的文字颜色
| default: 'inherit'
| },
| // 可选:增加尺寸控制
| size: {
| type: String,
| default: '1em'
| }
| },
| computed: {
| // 使用计算属性来动态生成样式
| svgStyle() {
| return {
| fontSize: this.size,
| };
| }
| },
| mounted() {
| console.log(config.activeBook.bookThemeColor, "987654");
| }
| }
| </script>
|
| <style scoped>
| .svg-icon {
| /*
| 1. width 和 height 使用 em 单位,使其与 font-size 关联。
| 2. overflow: hidden 防止图标在某些情况下溢出。
| 3. vertical-align: 调整图标与文字的对齐基线。
| 4. fill: currentColor; 告诉SVG内部所有没有指定fill的元素,
| 统一使用当前元素的 'color' 值作为填充色。
| */
| width: 1em;
| height: 1em;
| overflow: hidden;
| vertical-align: -0.15em;
| fill: currentColor;
| }
|
| </style>
|
|