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

Vue3使用icon的兩種方式實(shí)例

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

技術(shù)棧和版本 Vite2 + Vue3 + fontAwesome5

Vue3 中使用Icon的方式,fontAwesome 簡(jiǎn)單好用,但有時(shí)候缺少想要的icon。采用svg的方式,想要什么,直接下載,然后使用,種類(lèi)更加的全,基本上沒(méi)有不符合需求的icon,但是沒(méi)有fontAwesome 相對(duì)的容易輕松,每次都要下載對(duì)應(yīng)的圖片。

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 全局注冊(cè) main.js

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

e 創(chuàng)建導(dǎo)入處理函數(shù) 在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導(dǎo)入到配置文件

修改vite.config.js

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

export default defineConfig({
plugins: [
      vue(),
      //調(diào)用對(duì)應(yīng)的函數(shù) 進(jìn)行 svg 處理
      svgBuilder('./src/icons/')//對(duì)應(yīng)的文件夾,否則無(wú)法找到
    ]
})

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 全局注冊(cè)

    //按需導(dǎo)入
    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)
    // 全部導(dǎo)入
    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 使用

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

3  來(lái)源

來(lái)源  fontAwesome http://m.fzitv.net/article/228944.htm

來(lái)源  svg  http://m.fzitv.net/article/228948.htm

4 總結(jié)

確定對(duì)應(yīng)的技術(shù)棧和版本,按照步驟,就沒(méi)什么問(wèn)題。

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

相關(guān)文章

最新評(píng)論

神农架林区| 合江县| 晋宁县| 临朐县| 临清市| 德阳市| 达州市| 北碚区| 涞水县| 梁山县| 理塘县| 阳高县| 明星| 岚皋县| 南安市| 建德市| 呼和浩特市| 子洲县| 霍林郭勒市| 新兴县| 华亭县| 化德县| 湟源县| 玉龙| 孟村| 陈巴尔虎旗| 睢宁县| 昭觉县| 乌兰浩特市| 南投县| 民乐县| 潞西市| 福州市| 理塘县| 平泉县| 上高县| 通河县| 龙州县| 闸北区| 南召县| 盘山县|