最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

教你在vue?中使用?svg?symbols

 更新時間:2022年08月20日 10:54:34   作者:韭五后  
這篇文章主要介紹了如何在?vue?中使用?svg?symbols,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

安裝svg-sprite-loader插件

npm install svg-sprite-loader -D

或者

yarn add svg-sprite-loader -D

在vue.config.js中配置 svg-sprite-loader

const path = require('path');
module.exports = {
  chainWebpack:config =>{
    const dir = path.resolve(__dirname,'src/assets/icons')
    config.module
      .rule('svg-sprite')
      .test(/\.svg$/)
      .include.add(dir).end() //設(shè)置 icons 目錄走 svg-sprite 規(guī)則
      .use('svg-sprite-loader').loader('svg-sprite-loader').options({extract : false}).end()
      .use('svgo-loader').loader('svgo-loader')
      .tap(options => ({...options,plugins:[{removeAttrs:{attrs:'fill'}}]})).end()//安裝插件去除svg中的fill屬性
    config.plugin('svg-sprite').use(require('svg-sprite-loader/plugin'),[{plainSprite: true}])
    config.module.rule('svg').exclude.add(dir)//其他svg loader 排除 icons 目錄
  }
}

也可以這么配置

const { defineConfig } = require('@vue/cli-service')
 
// 加在頭部
const path = require('path')
 
 
function resolve (dir) {
  return path.join(__dirname, dir)
}
 
 
module.exports = defineConfig({
  transpileDependencies: true,
  chainWebpack (config) {
    config.plugins.delete('prefetch')
 
    // set svg-sprite-loader
    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]'
        symbolId: '[name]'
      })
      .end()
    }
  
})

注意:src/assets/icons這個文件夾放著你的svg文件

封裝IconSvg.vue通用組件

<template>
  <div class="icon-wrapper">
    <svg class="icon" aria-hidden="true">
      <use :href="iconName" rel="external nofollow" ></use>
    </svg>
  </div>
</template>
 
<script>
export default {
  name: 'IconSvg',
  props: {
    name: String,
    prefix: {
      type: String,
      default: 'icon-'
    }
  },
  computed: {
    iconName() {
      // return `#${this.prefix}${this.name}`
      return `#${this.name}`
    }
  }
}
</script>
 
<style scoped>
.icon-wrapper {
  display: inline-block;
}
.icon {
  width: 100%;
  height: 100%;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

加載所有的svg靜態(tài)資源,并且把IconSvg注冊到全局組件當中

import Vue from 'vue'
import App from './App.vue'
import router from './router'
 
// 引入本地的svg文件
// 定義一個加載目錄的函數(shù)
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./assets/icons', false, /\.svg$/)
// 加載目錄下的所有的 svg 文件
requireAll(req)
 
// 全局注冊IconSvg組件
const IconSvg = () => import('./components/svg/IconSvg');
const components = {
  IconSvg
}
Object.keys(components).forEach(item => {
  Vue.component(item, components[item])
})
 
Vue.config.productionTip = false
 
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

使用

使用的話有多種形式,我們一個一個來看。

1、使用自己編寫的svg文件

在src/assets/icons文件夾下面創(chuàng)建circle.svg(一定是這個文件夾,因為這個文件夾下的svg文件會被你的svg插件解析)

<?xml version="1.0"?>
<svg viewBox="0 0 120 120" version="1.1"
  xmlns="http://www.w3.org/2000/svg">
  <circle cx="60" cy="60" r="50"/>
</svg>

經(jīng)過svg-sprite-loader插件的解析,你在html里面可以看到有個id為circle(和svg名稱一樣)的一個symbol標簽掛載在svg標簽下面

這個時候直接使用就可以了

<template>
  <div class="home">
    <IconSvg name="circle"/>
  </div>
</template>
 
<script>
 
export default {
  name: 'HomeView',
  components: {
  }
}
</script>
 
<style scoped>
 
</style>

 

2、將svg標簽封裝到一個vue文件中

創(chuàng)建SvgList.vue組件

<template>
  <svg width="0" height="0" preserveAspectRatio="none">
    <defs>
      <!-- 這是個圓 -->
      <symbol id="icon-circle" viewBox="0 0 200 200">
        <circle cx="60" cy="60" r="50" />
      </symbol>
      <!-- 這是個動畫 -->
      <symbol id="icon-circle" viewBox="0 0 200 200">
        <rect width="10" height="10">
          <animate attributeName="rx" values="0;5;0" dur="10s" repeatCount="indefinite" />
        </rect>
      </symbol>
      <!-- 這是個正方形 -->
      <symbol id="icon-circle" viewBox="0 0 200 200">
        <rect x="10" y="10" width="100" height="100"/>
      </symbol>
    </defs>
  </svg>
</template>
 
<script>
export default {
 
};

使用如下

<template>
  <div class="home">
    <!-- 這里要使用這個組件,否則svg標簽引入不進來 -->
    <svg-list />
    <IconSvg name="icon-circle" class="icon1"/>
    <IconSvg name="icon-rect" class="icon1"/>
  </div>
</template>
 
<script>
import SvgList from '../components/SvgList.vue'
 
export default {
  name: 'HomeView',
  components: {
    SvgList
  }
}
</script>
 
<style scoped>
.icon1 {
  width: 100px;
  height:100px;
  color: red;
}
</style>

 

3、使用iconfont的svg代碼

在src/assets/icons這個文件夾下面去創(chuàng)建heart.svg(一定是這個文件夾,因為這個文件夾下的svg文件會被你的svg插件解析)

<svg t="1660895988570" class="icon" viewBox="0 0 1171 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1570" width="200" height="200">
  <path d="M1001.6 153.6s0-3.2 0 0c-118.4-118.4-304-121.6-425.6-12.8-121.6-108.8-307.2-105.6-422.4 9.6-118.4 118.4-121.6 310.4-3.2 428.8l3.2 3.2 355.2 352c38.4 38.4 99.2 38.4 137.6 0l355.2-352c118.4-118.4 118.4-310.4 0-428.8z m-115.2 249.6c-9.6 0-16-6.4-16-16 0-57.6-48-105.6-105.6-105.6-9.6 0-16-6.4-16-16s6.4-16 16-16c76.8 0 137.6 60.8 137.6 137.6 0 9.6-6.4 16-16 16z" fill="#343231" p-id="1571">
  </path>
</svg>

使用

<template>
  <div class="home">
    <IconSvg name="heart" class="icon1"/>
  </div>
</template>
 
<script>
 
export default {
  name: 'HomeView',
  components: {
  }
}
</script>
 
<style scoped>
.icon1 {
  width: 100px;
  height:100px;
  color: chocolate;
}
</style>

效果

在這里你會遇到一個問題,那就是給這個svg改顏色改不掉,你只要把svg標簽里的fill屬性去掉就可以了,或者把fill屬性設(shè)置成none。還有一個stroke輪廓顏色也是可以設(shè)置的。

4、直接引入整個iconfont的svg圖片

生成symbol代碼

 

在index.html中引入上面的symbol代碼

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico" rel="external nofollow" >
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
    
    <!-- 引入svg代碼 -->
    <script src="http://at.alicdn.com/t/c/font_1803779_bgkm96l1xgp.js"></script>
  </body>
</html>

看控制臺已經(jīng)全部引入進來了

使用

<template>
  <div class="home">
    <IconSvg name="iconjiahao" class="icon1"/>
    <IconSvg name="iconqicheqianlian-1" class="icon2"/>
    <IconSvg name="iconxinaixin" class="icon2"/>
    <IconSvg name="iconjinzhi" class="icon1"/>
    <IconSvg name="iconcart" class="icon1"/>
  </div>
</template>
 
<script>
 
export default {
  name: 'HomeView',
  components: {
  }
}
</script>
 
<style scoped>
.icon1 {
  width: 100px;
  height:100px;
  color: chocolate;
}
.icon2 {
  width: 100px;
  height:100px;
  color: darkorange;
}
</style>

效果

提示:當然你也可以上傳svg圖片到iconfont平臺,然后再下載下來使用,但是在下載下來的時候一定要去去除顏色并提交,這樣你的svg圖片就可以隨心所欲的改顏色了

到此這篇關(guān)于如何在vue中使用svgsymbols的文章就介紹到這了,更多相關(guān)vue使用svgsymbols內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue在data中定義變量后依舊報undefined的解決

    vue在data中定義變量后依舊報undefined的解決

    這篇文章主要介紹了vue在data中定義變量后依舊報undefined的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue 根據(jù)條件判斷van-tab的顯示方式

    Vue 根據(jù)條件判斷van-tab的顯示方式

    這篇文章主要介紹了Vue 根據(jù)條件判斷van-tab的顯示方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue中子組件傳遞數(shù)據(jù)給父組件的講解

    vue中子組件傳遞數(shù)據(jù)給父組件的講解

    今天小編就為大家分享一篇關(guān)于vue中子組件傳遞數(shù)據(jù)給父組件的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • ElementPlus表單rules校驗的方法步驟

    ElementPlus表單rules校驗的方法步驟

    相信大家經(jīng)常都會遇到要處理表單驗證的環(huán)節(jié),而我在最近的項目中也遇到需要做表單驗證的業(yè)務(wù),下面這篇文章主要給大家介紹了關(guān)于ElementPlus表單rules校驗的方法步驟,需要的朋友可以參考下
    2023-04-04
  • Vue子組件向父組件傳值示范方法

    Vue子組件向父組件傳值示范方法

    這篇文章主要介紹了Vue子組件向父組件傳值方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧
    2023-03-03
  • Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    這篇文章主要介紹了Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時),幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • Vue 中如何利用 new Date() 獲取當前時間

    Vue 中如何利用 new Date() 獲取當前時間

    在 Vue 開發(fā)中,利用 new Date() 方法可以方便地獲取當前時間,并通過 Date 對象的方法進行時間格式化和操作。通過本文的介紹,您應(yīng)該對在 Vue 中獲取當前時間有了更深入的了解,并了解了一些常見的時間操作方法,需要的朋友可以參考下
    2023-07-07
  • vue?ui的安裝步驟以及使用詳解

    vue?ui的安裝步驟以及使用詳解

    最近公司開發(fā)一個項目,采用的前后端分離的方式,前端采用vue,但是再項目開發(fā)過程中遇到一個比較麻煩的問題,下面這篇文章主要給大家介紹了關(guān)于vue?ui的安裝步驟以及使用的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Vue實現(xiàn)骨架屏的示例代碼

    Vue實現(xiàn)骨架屏的示例代碼

    骨架屏就是在頁面數(shù)據(jù)尚未加載前先給用戶展示出頁面的大致結(jié)構(gòu)。本文將利用Vue實現(xiàn)簡單的骨架屏,感興趣的小伙伴可以跟隨小編一起學(xué)習一下
    2023-03-03
  • Vue基于localStorage存儲信息代碼實例

    Vue基于localStorage存儲信息代碼實例

    這篇文章主要介紹了Vue基于localStorage存儲信息代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-11-11

最新評論

南充市| 浑源县| 四川省| 西乌珠穆沁旗| 碌曲县| 泰宁县| 泰宁县| 邢台县| 本溪| 西平县| 福清市| 介休市| 巍山| 哈巴河县| 托克逊县| 武冈市| 金堂县| 许昌市| 金湖县| 集安市| 高雄市| 沅陵县| 万荣县| 昌都县| 县级市| 奈曼旗| 漳浦县| 资中县| 新源县| 永安市| 新密市| 江孜县| 桓台县| 集安市| 青铜峡市| 中山市| 巴南区| 山阴县| 石棉县| 紫金县| 老河口市|