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

Vue3使用icon的兩種方式實例

 更新時間:2021年11月11日 15:19:51   作者:一文Booook  
vue開發(fā)網站的時候,往往圖標是起著很重要的作用,下面這篇文章主要給大家介紹了關于Vue3使用icon的兩種方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下

技術棧和版本 Vite2 + Vue3 + fontAwesome5

Vue3 中使用Icon的方式,fontAwesome 簡單好用,但有時候缺少想要的icon。采用svg的方式,想要什么,直接下載,然后使用,種類更加的全,基本上沒有不符合需求的icon,但是沒有fontAwesome 相對的容易輕松,每次都要下載對應的圖片。

1. 使用svg

a 下載需要使用的SVG圖片,保存至 src/icons文件夾

b 安裝依賴 fs 和svg的loader

命令:npm install svg-sprite-loader

命令:npm install --save fs

c 創(chuàng)建模板文件 index.vue

模板文件代碼

<template>
    <svg :class="svgClass" v-bind = "$attrs">
        <use :xlink:href="iconName"></use>
    </svg>
</template>


<script setup>

import { defineProps, computed } from "vue";

const props = defineProps({
    name: {
      type: String,
      required: true
    },
    color: {
      type: String ,
      default: '',
    }
})

const iconName = computed(()=>`#icon-${props.name}`);
const svgClass = computed(()=> {
    // console.log(props.name,'props.name');
    if (props.name) {
        return `svg-icon icon-${props.name}`
    }
      return 'svg-icon'
});

</script>

<style scoped lang ="scss">
    .svg-icon{
        width: 3em;
        height: 3em;
        fill: currentColor;
        border: solid 2px red;
        vertical-align: middle;
    }
</style>>

d 全局注冊 main.js

import { svgIcon } from './components'
.component('svg-icon',svgIcon)

e 創(chuàng)建導入處理函數 在plugins中創(chuàng)建 svgBulider.js

代碼

import { readFileSync, readdirSync } from 'fs'


let idPerfix = ''
const svgTitle = /<svg([^>+].*?)>/
const clearHeightWidth = /(width|height)="([^>+].*?)"/g

const hasViewBox = /(viewBox="[^>+].*?")/g

const clearReturn = /(\r)|(\n)/g

function findSvgFile(dir) {
  const svgRes = []
  const dirents = readdirSync(dir, {
    withFileTypes: true
  })
  for (const dirent of dirents) {
    if (dirent.isDirectory()) {
      svgRes.push(...findSvgFile(dir + dirent.name + '/'))
    } else {
      const svg = readFileSync(dir + dirent.name)
        .toString()
        .replace(clearReturn, '')
        .replace(svgTitle, ($1, $2) => {
        
          let width = 0
          let height = 0
          let content = $2.replace(
            clearHeightWidth,
            (s1, s2, s3) => {
              if (s2 === 'width') {
                width = s3
              } else if (s2 === 'height') {
                height = s3
              }
              return ''
            }
          )
          if (!hasViewBox.test($2)) {
            content += `viewBox="0 0 ${width} ${height}"`
          }
          return `<symbol id="${idPerfix}-${dirent.name.replace(
            '.svg',
            ''
          )}" ${content}>`
        })
        .replace('</svg>', '</symbol>')
      svgRes.push(svg)
    }
  }
  return svgRes
}

export const svgBuilder = (path, perfix = 'icon') => {
  if (path === '') return
  idPerfix = perfix
  const res = findSvgFile(path)
  
  return {
    name: 'svg-transform',
    transformIndexHtml(html) {
      return html.replace(
        '<body>',
        `
          <body>
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position: absolute; width: 0; height: 0">
              ${res.join('')}
            </svg>
        `
      )
    }
  }
}

f 修改配置文件,將svgBulider導入到配置文件

修改vite.config.js

import { svgBuilder } from './src/plugins/svgBuilder'; '

export default defineConfig({
plugins: [
      vue(),
      //調用對應的函數 進行 svg 處理
      svgBuilder('./src/icons/')//對應的文件夾,否則無法找到
    ]
})

g 使用SVG

核心部分

  <svg-icon name="questionmark" />//name 取你的svg圖片

2. 使用fontAwesome

a npm 安裝依賴

$ npm i --save @fortawesome/fontawesome
$ npm i --save @fortawesome/vue-fontawesome

$ npm i --save @fortawesome/fontawesome-free-solid
$ npm i --save @fortawesome/fontawesome-free-regular
$ npm i --save @fortawesome/fontawesome-free-brands

b mian.js 全局注冊

    //按需導入
    import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
    import { library } from '@fortawesome/fontawesome-svg-core'
    import { faAd } from '@fortawesome/free-solid-svg-icons'
    
    import { faAddressBook } from '@fortawesome/free-solid-svg-icons'
    
    library.add(faAddressBook)
    // 全部導入
    import { fas } from'@fortawesome/free-solid-svg-icons'
    import { fab } from '@fortawesome/free-brands-svg-icons'
    library.add(fas,fab)
    
    .component('font-awesome-icon', FontAwesomeIcon)
    

c 使用

    //按需導入的使用
      <font-awesome-icon  icon="address-book" class="fa-spin fa-lg"/>
    //全局導入的使用
      <font-awesome-icon  :icon="['fas','address-book']" />

3  來源

來源  fontAwesome http://www.fzitv.net/article/228944.htm

來源  svg  http://www.fzitv.net/article/228948.htm

4 總結

確定對應的技術棧和版本,按照步驟,就沒什么問題。

到此這篇關于Vue3使用icon的兩種方式的文章就介紹到這了,更多相關Vue3使用icon內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue 二維碼長按保存和復制內容操作

    vue 二維碼長按保存和復制內容操作

    這篇文章主要介紹了vue 二維碼長按保存和復制內容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue3?TS?vite?element?ali-oss使用教程示例

    vue3?TS?vite?element?ali-oss使用教程示例

    這篇文章主要為大家介紹了vue3?TS?vite?element?ali-oss使用教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • vue?watch報錯:Error?in?callback?for?watcher?"xxx":"TypeError的解決方法

    vue?watch報錯:Error?in?callback?for?watcher?"xxx&qu

    這篇文章主要給大家介紹了關于vue?watch報錯:Error?in?callback?for?watcher?“xxx“:“TypeError:Cannot?read?properties?of?undefined的解決方法,需要的朋友可以參考下
    2023-03-03
  • vue?element如何添加遮罩層

    vue?element如何添加遮罩層

    這篇文章主要介紹了vue?element如何添加遮罩層問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 解決v-model雙向綁定失效的問題

    解決v-model雙向綁定失效的問題

    這篇文章主要介紹了解決v-model雙向綁定失效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue3中動態(tài)修改樣式與級聯樣式優(yōu)先順序圖文詳解

    Vue3中動態(tài)修改樣式與級聯樣式優(yōu)先順序圖文詳解

    在項目中,我們時常會遇到動態(tài)的去綁定操作切換不同的CSS樣式,下面這篇文章主要給大家介紹了關于Vue3中動態(tài)修改樣式與級聯樣式優(yōu)先順序的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • Vue+Vite項目初建(axios+Unocss+iconify)的實現

    Vue+Vite項目初建(axios+Unocss+iconify)的實現

    一個好的項目開始搭建總是需要配置許多初始化配置,本文就來介紹一下Vue+Vite項目初建(axios+Unocss+iconify)的實現,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Vue路由跳轉與接收參數的實現方式

    Vue路由跳轉與接收參數的實現方式

    這篇文章主要介紹了Vue路由跳轉與接收參數的實現方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • vue解析Json數據獲取Json里面的多個id問題

    vue解析Json數據獲取Json里面的多個id問題

    這篇文章主要介紹了vue解析Json數據獲取Json里面的多個id問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 公共Hooks封裝文件下載useDownloadFile實例詳解

    公共Hooks封裝文件下載useDownloadFile實例詳解

    這篇文章主要為大家介紹了公共Hooks封裝文件下載useDownloadFile實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

渑池县| 大洼县| 万宁市| 淮南市| 夏邑县| 永新县| 湛江市| 固始县| 郎溪县| 丹东市| 宽甸| 勃利县| 武安市| 三江| 黄大仙区| 葫芦岛市| 彰武县| 扶风县| 永顺县| 延吉市| 吴忠市| 宣恩县| 竹北市| 郓城县| 噶尔县| 闽侯县| 汉源县| 萝北县| 北流市| 博野县| 上高县| 理塘县| 保定市| 新昌县| 高雄市| 阳江市| 玉龙| 甘南县| 呈贡县| 镇巴县| 林甸县|