首页 > 其他分享 >vue中如何使用svg,以及碰到的相应问题

vue中如何使用svg,以及碰到的相应问题

时间:2023-10-25 12:44:38浏览次数:32  
标签:碰到 vue name icons svg js icon 图标

安装

cnpm install svg-sprite-loader --save-dev

创建svg文件夹存放svg图标

创建icons文件夹,在icons文件夹下创建svg文件夹存放本地svg图标。
image

vue.config.js 中配置svg图片

 config.module.rule("svg").exclude.add(resolve("src/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "[name]", //注意这里,可能会导致svg不显示,要与vue文件中的name一致
      })
     .end();

创建SvgIcon公用组件

<template>
  <svg :class="getClassName" :width="width" :height="height" aria-hidden="true">
    <use :xlink:href="getName"></use>
  </svg>
</template>

<script>
export default {
  name: "icon-svg",
  props: {
    name: {
      type: String,
      required: true,
    },
    className: {
      type: String,
    },
    width: {
      type: String,
    },
    height: {
      type: String,
    },
  },
  computed: {
    getName() {
      return `#icon-${this.name}`;
    },
    getClassName() {
      if (this.className) {
        return "icon-svg svg-icon " + this.className;
      } else {
        return "icon-svg svg-icon";
      }
    },
  },
};
</script>

<style>
.icon-svg {
  width: 1em;
  height: 1em;
  fill: currentColor;
  overflow: hidden;
}
</style>

创建index.js 导入所有svg图标

icons文件夹创建index.js 自动导入所有svg图标。

/**
 * 字体图标, 统一使用SVG Sprite矢量图标(http://www.iconfont.cn/)
 *
 * 使用:
 *  1. 在阿里矢量图标站创建一个项目, 并添加图标(这一步非必须, 创建方便项目图标管理)
 *  2-1. 添加icon, 选中新增的icon图标, 复制代码 -> 下载 -> SVG下载 -> 粘贴代码(重命名)
 *  2-2. 添加icons, 下载图标库对应[iconfont.js]文件, 替换项目[./iconfont.js]文件
 *  3. 组件模版中使用 [<icon-svg name="canyin"></icon-svg>]
 *
 * 注意:
 *  1. 通过2-2 添加icons, getNameList方法无法返回对应数据
 */
import Vue from "vue";
import IconSvg from "@/components/icon-svg";

Vue.component("IconSvg", IconSvg);
// register globally

const req = require.context("./svg", false, /\.svg$/);
const requireAll = (requireContext) =>
requireContext.keys().map(requireContext);

执行 requireContext.keys().map(requireContext)得到返回的数据:
image

main.js中引入icons/index.js

image

使用

image

问题解决

https://blog.csdn.net/qq_35119405/article/details/106115244

标签:碰到,vue,name,icons,svg,js,icon,图标
From: https://www.cnblogs.com/guozhiqiang/p/17786898.html

相关文章

  • vue3 elementplus table表格内添加checkbox和行号
    1.仅添加复选框<el-table-columntype="selection"width="55"></el-table-column>2.添加复选框和文字行号在一列<el-table-column><template#header><el-checkboxv-model="selectAll"@change="han......
  • vue 首次加载项目,控制台报错: Redirected when going from "/" to "/login"
    第一次加载加载页面时报错如下:Redirectedwhengoingfrom"/"to"/login"viaanavigationguard. ![image](https://img2023.cnblogs.com/blog/1880163/202310/1880163-20231025113840444-1010075971.png)后续在地址栏直接添加/login,index,错误页面等均正常无报错.路由......
  • Jenkins配置java和vue构建环境
    jdk,maven,node,localtime等配置可通过挂载的方式进行配置前提条件是虚拟机中已配置好jdk,maven,node等环境注意自己虚拟机相关环境配置的路径以下样例为我自己的虚拟机中的配置路径-v宿主机(虚拟机)路径:容器路径dockerrun--namejenkins-p28081:8080-p50000:50000-v/v......
  • vue3 动态加载组件
    <el-dropdownstyle="margin:0px"><el-buttontype="primary">视图</el-button><template#dropdown><el-dropdown-menu><el-dropdown-itemv-for="dropItemindropI......
  • js中使用css变量(vue)
    html<divclass="test":style="{'--backgroundColor':backgroundColor}"></div>jscss .test{background-color:var(--backgroundColor);} ......
  • 28-Vue脚手架-props配置项
    props配置项props让组件接收外部传过来的数据1)传递数据这里需要注意,通过age="18"的方式,传递进去的数据是字符类型的,通过动态绑定:age="26"的方式,传递进去的数据是整型类型<!--这里需要注意,:age="26"代表v-bind动态绑定,传入的age是int类型--><Studentname="马铃薯......
  • Vue无线滚动不触发问题
    Vue的v-infinite-scroll="load" 会无线触发  要定义实际高度和可视高度之间关系设置style="overflow:auto;height:calc(100vh - 49px)"  100vh = 100% <divclass="box"style="overflow:auto;height:calc(100vh-100px)"><divv......
  • 转载 vue-cli5降级为vue-cli4
    vue-cli5降级为vue-cli41.启动命令行,运行npmconfigls-l,找到userconfig路径下的.npmrc文件进行删除;2.输入wherevue,把vuevue.cmd这两个文件删除;3.输入vue-V,会显示‘vue’不是内部或外部命令,也不是可运行的程序或批处理文件。4.运行npmunivue-cli-g;5.安装任意指定......
  • Vue
    Vue一套前端框架,免除原生JavaScript中的DOM操作,简化书写基于MVVM(Model-View-ViewModel)思想,实现数据的双向绑定,将编程的关注点放在数据上框架:是一个半成品软件,是一套可重用的、通用的、软件基础代码模型。基于框架进行开发,更加快捷,更加高效。<head></head>中引入官方......
  • 记录--vue3实现excel文件预览和打印
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前言在前端开发中,有时候一些业务场景中,我们有需求要去实现excel的预览和打印功能,本文在vue3中如何实现Excel文件的预览和打印。预览excel关于实现excel文档在线预览的做法,一种方式是通过讲文档里的数据处理成......