亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

教你在vue項(xiàng)目中使用svg圖標(biāo)的方法

 更新時(shí)間:2022年04月20日 10:25:06   作者:小方塊的世界  
本文給大家介紹了在vue項(xiàng)目中使用svg圖標(biāo)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
  • svg圖標(biāo)優(yōu)點(diǎn)

svg與iconfont之類(lèi)的字體圖標(biāo)在網(wǎng)頁(yè)中的使用差別不大,可以修改大小,顏色等而且不失真。

  • 安裝svg-sprite-loader
npm install --save-dev svg-sprite-loader
  • 文件夾目錄 (xxx.svg 注意:這里的 xxx 不要使用中文)
- assets
-- icon
--- svg
--- index.js
  • 配置依賴(lài)
// Vue2.x 在 webpack.base.conf.js 中配置如下:
// 注意svg圖標(biāo)的路徑 src/assets/icon 要寫(xiě)正確
 module: {
    rules: [
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        include: [resolve('src/assets/icon')],
        options: {
          symbolId: 'icon-[name]'
        }
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        exclude: [resolve('src/assets/icon')],
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      }
    ]
  }
// Vue3.x 在根目錄新建 vue.config.js 中配置如下:
module.exports = {
  chainWebpack: config => {
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule.exclude.add(/node_modules/)
    svgRule
      .test(/\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })

    // 修改images loader 添加svg處理
    const imagesRule = config.module.rule('images')
    imagesRule.exclude.add(resolve('src/assets/icon'))
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
  }
}
// Vue4.x 在根目錄新建 vue.config.js 中配置如下:
const path = require('path')
module.exports = {
  // 使用svg-sprite-loader編譯svg,若使用file-loader時(shí)排除src/icon下的svg矢量圖標(biāo)
  chainWebpack: (config) => {
    const svgRule = config.module.rule('svg')
    // 清除已有的所有 loader 否則接下來(lái)的 loader 會(huì)附加在該規(guī)則現(xiàn)有的 loader 之后
    svgRule.uses.clear()
    svgRule
      .test(/\.svg$/)
      .include.add(path.resolve(__dirname, './src/icon'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
    const fileRule = config.module.rule('file')
    fileRule.uses.clear()
    fileRule
      .test(/\.svg$/)
      .exclude.add(path.resolve(__dirname, './src/icon'))
      .end()
      .use('file-loader')
      .loader('file-loader')
  },
}

  • 在components目錄下增加一個(gè)SvgIcon組件
<template>
  <div
    v-if="isExternal"
    :style="styleExternalIcon"
    class="svg-external-icon svg-icon"
    v-on="$listeners"
  />
  <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="iconName" rel="external nofollow"  />
  </svg>
</template>

<script>
// 檢查是否是外部鏈接
var isExternal function(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export default {
  name: "SvgIcon",
  props: {
    iconClass: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: "",
    },
  },
  computed: {
    isExternal() {
      return isExternal(this.iconClass)
    },
    iconName() {
      return `#icon-${this.iconClass}`
    },
    svgClass() {
      if (this.className) {
        return "svg-icon " + this.className
      } else {
        return "svg-icon"
      }
    },
    styleExternalIcon() {
      return {
        mask: `url(${this.iconClass}) no-repeat 50% 50%`,
        "-webkit-mask": `url(${this.iconClass}) no-repeat 50% 50%`,
      }
    }
  }
}
</script>

<style scoped>
    .svg-icon {
      width: 1em;
      height: 1em;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }

    .svg-external-icon {
      background-color: currentColor;
      mask-size: cover !important;
      display: inline-block;
    }
</style>
  • icon文件夾下index.js中導(dǎo)入所有svg文件,并將SvgIcon注冊(cè)到全局
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'

// 全局注冊(cè)
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)
  • main.js文件中引入 svg 配置
import '@/assets/icon'
  • 使用
<!-- 其中icon-class對(duì)應(yīng)圖標(biāo)svg文件的名稱(chēng);className對(duì)應(yīng)圖標(biāo)的css樣式屬性 -->
<svg-icon icon-class="arrow" className="left-arrow"></svg-icon>
  • 備注

若svg圖標(biāo)不能通過(guò)樣式修改顏色,打開(kāi)svg文件,刪除style標(biāo)簽里的每一項(xiàng)fill樣式設(shè)置。但是如果svg文件中使用的不是 path 那就沒(méi)有辦法了。比如有些在線(xiàn)的工具可以把圖片轉(zhuǎn)成svg格式,轉(zhuǎn)換后svg文件中的地址是 base64 ,這種的就不能改變樣式了,而且放縮也會(huì)失真。

到此這篇關(guān)于教你在vue項(xiàng)目中使用svg圖標(biāo)的方法的文章就介紹到這了,更多相關(guān)vue使用svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論