vue中使用use引入的svg怎么添加title

项目框架:

vue2

使用场景:

我们项目中的svg文件比较多,每个都copy里面的svg代码的话,会造成需要写很多个vue文件,于是乎当时采用了use的方式引入了svg文件

代码如下(当然中间省去了其他步骤,use方式引入svg不是本篇重点)

html 复制代码
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName"/>
  </svg>
</template>
遇到的问题

每个icon好像默认使用了文件名称作为title,也就是悬浮上去提示的文字。所以需要添加title

svg直接添加title是无效的,必须使用svg自己的title标签,直接添加到use的外面也是无效的,如下示例

html 复制代码
<!-- 无效代码1 -->
<template>
  <svg :class="svgClass" aria-hidden="true" :title="title">
    <use :xlink:href="iconName"/>
  </svg>
</template>

<!-- 无效代码2 -->
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName" :title="title"/>
  </svg>
</template>

<!-- 无效代码3 -->
<template>
  <svg :class="svgClass" aria-hidden="true">
    <title v-if="title">{{ title }}</title>
    <use :xlink:href="iconName"/>
  </svg>
</template>
最后的实现

需要加在use的里面

html 复制代码
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName">
      <title v-if="title">{{ title }}</title>
    </use>
  </svg>
</template>
相关推荐
大黄说说7 小时前
HTML5语义化标签:从div到article与section的进化之路
前端·html·html5
帅小伙―苏7 小时前
力扣42接雨水
前端·算法·leetcode
糯米团子7497 小时前
react速通-2
前端·react.js·前端框架
糯米团子7497 小时前
react速通-3
javascript·react.js·前端框架
心连欣7 小时前
从静态页面到动态交互:DOM操作的核心API解析
前端·javascript·api
橙某人7 小时前
SSR页面上的按钮点不了?Nuxt 懒加载水合揭秘💧
前端·vue.js·nuxt.js
零瓶水Herwt7 小时前
Javascript常用设计模式
javascript
PursuitofHappiness7 小时前
2 tree-cli 的使用方法
前端
风骏时光牛马7 小时前
C Shell脚本编程与系统管理技术实践指南
javascript
烛衔溟7 小时前
TypeScript this 参数类型与全局 this
javascript·ubuntu·typescript