欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中引入svg圖標(biāo)的兩種方式

 更新時(shí)間:2021年01月14日 10:16:07   作者:十里不故夢(mèng)  
這篇文章主要給大家介紹了關(guān)于Vue中引入svg圖標(biāo)的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Vue中引入svg圖標(biāo)的方式

Vue中引入svg圖標(biāo)的方式一

安裝

yarn add svg-sprite-loader --dev

svg組件

index.vue

<!-- svg組件 -->
<template>
 <svg class="svg-icon" :class="svgClass" aria-hidden="true">
  <use :xlink:href="iconName" />
 </svg>
</template>

<script>
export default {
 name: 'SvgIcon',
 props: {
  // svg 的名稱
  svgName: {
   type: String,
   required: true
  }
 },
 computed: {
  iconName () {
   return `#icon-${this.svgName}`
  },
  svgClass () {
   if (this.svgName) {
    return 'svg-icon' + this.svgName
   } else {
    return 'svg-icon'
   }
  }
 }
}
</script>

<style lang="less" scoped>
.svg-icon {
 width: 100px;
 height: 100px;
 vertical-align: -0.15em;
 fill: currentColor;
 overflow: hidden;
}
</style>

注冊(cè)到全局

index.js

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'

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

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

vue.config.js

module.exports = {
	chainWebpack: config => {
	 	config.module
   .rule('svg')
   .exclude.add(resolve('src/assets/icons'))
   .end()
  config.module
   .rule('icons')
   .test(/\.svg$/)
   .include.add(resolve('src/assets/icons'))
   .end()
   .use('svg-sprite-loader')
   .loader('svg-sprite-loader')
   .options({
    symbolId: 'icon-[name]'
   })
   .end()
	}  
}

頁(yè)面中使用

<!-- svg-name為svg名 -->
<svg-icon svg-name="ic_home_news" />

Vue中引入svg圖標(biāo)的方式二

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

vue.config.js中添加如下代碼

const path = require('path');
function resolve(dir) {
 // __dirname項(xiàng)目根目錄的絕對(duì)路徑
 return path.join(__dirname, dir);
}
module.exports = {
 chainWebpack: config => {
 const svgRule = config.module.rule('svg');
 // 清除已有的所有l(wèi)oader
 // 如果你不這樣做,接下來(lái)的loader會(huì)附加在該規(guī)則現(xiàn)有的loader之后
 svgRule.uses.clear();
 svgRule
  .test(/\.svg$/)
  .include.add(path.resolve(__dirname, './src/icons/svg'))
  .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/icons/svg'))
  .end()
  .use('file-loader')
  .loader('file-loader');
 },
}

建立如下的文件目錄

SvgIcon.vue代碼

<template>
 <svg :class="svgClass" xmlns="http://www.w3.org/2000/svg">
 <use :xlink:href="iconName" xmlns:xlink="http://www.w3.org/1999/xlink" />
 </svg>
</template>
<script>
export default {
 name: 'SvgIcon',
 props: {
 iconClass: {
  type: String,
  required: true
 },
 className: {
  type: String,
  default: ''
 }
 },
 computed: {
 iconName() {
  return `#icon-${this.iconClass}`;
 },
 svgClass() {
  if (this.className) {
  return 'svg-icon ' + this.className;
  } else {
  return 'svg-icon';
  }
 }
 }
};
</script>
<style scoped>
.svg-icon {
 width: 1em;
 height: 1em;
 vertical-align: -0.15em;
 fill: currentColor;
 overflow: hidden;
}
</style>

svg文件夾下放svg圖標(biāo)

index.js代碼

import Vue from 'vue';
import SvgIcon from '@/components/SvgIcon'; // svg組件
// register globally
Vue.component('svg-icon', SvgIcon);
const req = require.context('./svg', false, /\.svg$/);
const requireAll = requireContext => requireContext.keys().map(requireContext);
requireAll(req);

最后在main.js中引入

import './icons'; 

在頁(yè)面中使用svg

icon-class是svg圖標(biāo)名 class-name是你要自定義的class類名

<svg-icon icon-class="features_ic_risk@1x" class-name="icon"></svg-icon>

總結(jié)

到此這篇關(guān)于Vue中引入svg圖標(biāo)的兩種方式的文章就介紹到這了,更多相關(guān)Vue引入svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue自定義權(quán)限標(biāo)簽詳細(xì)代碼示例

    vue自定義權(quán)限標(biāo)簽詳細(xì)代碼示例

    這篇文章主要給大家介紹了關(guān)于vue自定義權(quán)限標(biāo)簽的相關(guān)資料,在Vue中你可以通過(guò)創(chuàng)建自定義組件來(lái)實(shí)現(xiàn)自定義標(biāo)簽組件,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-09-09
  • Vue中對(duì)iframe實(shí)現(xiàn)keep alive無(wú)刷新的方法

    Vue中對(duì)iframe實(shí)現(xiàn)keep alive無(wú)刷新的方法

    這篇文章主要介紹了Vue中對(duì)iframe實(shí)現(xiàn)keep alive無(wú)刷新的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • vue3與webpack5安裝element-plus樣式webpack編譯報(bào)錯(cuò)問(wèn)題解決

    vue3與webpack5安裝element-plus樣式webpack編譯報(bào)錯(cuò)問(wèn)題解決

    這篇文章主要介紹了vue3與webpack5安裝element-plus樣式webpack編譯報(bào)錯(cuò),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • vue 需求 data中的數(shù)據(jù)之間的調(diào)用操作

    vue 需求 data中的數(shù)據(jù)之間的調(diào)用操作

    這篇文章主要介紹了vue 需求 data中的數(shù)據(jù)之間的調(diào)用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Vue2和Vue3的10種組件通信方式梳理

    Vue2和Vue3的10種組件通信方式梳理

    這篇文章主要介紹了Vue2和Vue3的10種組件通信方式梳理,本文將通過(guò)選項(xiàng)式API?組合式API以及setup三種不同實(shí)現(xiàn)方式全面介紹Vue2和Vue3的組件通信方式,需要的朋友可以參考一下
    2022-08-08
  • vue3?element?plus按需引入最優(yōu)雅的用法實(shí)例

    vue3?element?plus按需引入最優(yōu)雅的用法實(shí)例

    這篇文章主要給大家介紹了關(guān)于vue3?element?plus按需引入最優(yōu)雅的用法,以及關(guān)于Element-plus按需引入的一些坑,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • vue中點(diǎn)擊按鈕下載文件的實(shí)現(xiàn)方式

    vue中點(diǎn)擊按鈕下載文件的實(shí)現(xiàn)方式

    這篇文章主要介紹了vue中點(diǎn)擊按鈕下載文件的實(shí)現(xiàn)方式,具有很的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 關(guān)于element-ui resetFields重置方法無(wú)效問(wèn)題及解決

    關(guān)于element-ui resetFields重置方法無(wú)效問(wèn)題及解決

    這篇文章主要介紹了關(guān)于element-ui resetFields重置方法無(wú)效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue預(yù)覽圖片和視頻的幾種實(shí)現(xiàn)方式

    Vue預(yù)覽圖片和視頻的幾種實(shí)現(xiàn)方式

    本文主要介紹了Vue預(yù)覽圖片和視頻的幾種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • vue3-vite安裝后main.ts文件和tsconfig.app.json文件報(bào)錯(cuò)解決辦法

    vue3-vite安裝后main.ts文件和tsconfig.app.json文件報(bào)錯(cuò)解決辦法

    Vue.js是一個(gè)流行的JavaScript框架,它可以幫助開發(fā)者構(gòu)建交互式Web應(yīng)用程序,這篇文章主要給大家介紹了關(guān)于vue3-vite安裝后main.ts文件和tsconfig.app.json文件報(bào)錯(cuò)解決辦法,需要的朋友可以參考下
    2023-12-12

最新評(píng)論