放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 前端

    • 前端 概览
    • 前端汇总

    • front 博文

    • front 项目总结

    • front 高级

    • front tools

  • vue

    • vue 概览
    • vue 汇总

    • vue 博文

    • vue 项目总结

      • vue 配置
      • vue环境
      • vue国际化
      • vue指令
      • vue页面刷新
      • vue混入
      • vue-other
      • vue二维码
      • vue防抖节流
      • vue svg
        • vue 中 svg 的使用
          • 1. 安装依赖
          • 2. 添加配置
          • 3.创建公共组件
          • 4.创建 svg 文件夹
          • 5. 使用
        • vue中使用svg遇到问题
          • 1. vue-cli3运行时会加载node_modules下的svg报错问题
      • vue图片
      • vue echarts图表
      • vue复制
    • vue 高级

  • html

    • html 概览
    • html 汇总

    • html 博文

  • css

    • css 概览
    • css 汇总

    • css 博文

    • sass

    • less

  • js

    • javascript 概览
    • JS 汇总

    • ES6

    • JS 博文

    • JS 工具

  • node

    • node 概览
    • node 汇总

    • node 框架

    • node 博文

  • react

    • react 概览
    • react 汇总

    • react 博文

    • react 高级

  • 微信小程序

    • 微信小程序 概览
    • 微信小程序总结
    • 微信小程序文章
    • 微信小程序 博文

    • 微信小程序 高级

  • 微信公众号

    • 微信公众号 概览
    • 微信公众号总结
    • 微信公众号文章
  • 多端开发

    • 多端开发
    • dsbridge 概览
    • jsbridge 概览
    • webview
    • uniapp

      • uniapp 概览
    • taro

      • taro 概览
    • flutter

      • flutter 概览
      • flutter 环境搭建
    • electron

      • electron 概览
  • front
放肆青春
2020-07-20

vue svg

# vue 中 svg 的使用

# 1. 安装依赖

npm install svg-sprite-loader --save-dev

# 2. 添加配置

vue.config.js

 chainWebpack: (config) => {
    // svg是个基础loader
    const svgRule = config.module.rule("svg");

    // 清除已有的所有 loader。
    // 如果你不这样做,接下来的 loader 会附加在该规则现有的 loader 之后。
    svgRule.uses.clear();

    // 添加要替换的 loader
    svgRule
      .test(/\.svg$/)
      .include.add(resolve('src/icons')).end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .tap((options) => {
        // eslint-disable-next-line no-param-reassign
        options = {
          symbolId: 'icon-[name]',
        };
        return options;
      });
  },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 3.创建公共组件

components/IconSvg.vue

<template>
  <svg class="svg-icon" :style="iconStyle" aria-hidden="true">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
export default {
  name: "icon-svg",
  props: {
    iconClass: {
      type: String,
      required: true,
    },
    iconStyle: {
      type: String,
      default: "",
    },
  },
  computed: {
    iconName() {
      return `#icon-${this.iconClass}`;
    },
  },
};
</script>

<style>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>
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

# 4.创建 svg 文件夹

icons/index.js

import Vue from "vue";
//引入svg组件
import IconSvg from "@/components/IconSvg";

//全局注册icon-svg
Vue.component("icon-svg", IconSvg);

const requireAll = (requireContext) =>
  requireContext.keys().map(requireContext);
const req = require.context("./svg", false, /\.svg$/);
requireAll(req);
1
2
3
4
5
6
7
8
9
10
11

icons/svg/***svg 存放 svg 文件

# 5. 使用

<icon-svg iconClass="title" iconStyle="width:100px;height:100px;"></icon-svg>

# vue中使用svg遇到问题

# 1. vue-cli3运行时会加载node_modules下的svg报错问题

解决办法:依赖的npm包(mavon-editor)使用main.js全局导入,而不是页面中局部import:

import mavonEditor from 'mavon-editor';

Vue.use(mavonEditor)
1
2
3
更新时间: 3/22/2021, 4:43:19 PM
vue防抖节流
vue图片

← vue防抖节流 vue图片→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式