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

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://chabaoo.cn/article/228944.htm

來源  svg  http://chabaoo.cn/article/228948.htm

4 總結

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

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

相關文章

  • Vue封裝全局toast組件的完整實例

    Vue封裝全局toast組件的完整實例

    組件(Component)是 Vue.js 最強大的功能之一,組件可以擴展 HTML 元素,封裝可重用的代碼,這篇文章主要給大家介紹了關于Vue封裝全局toast組件,需要的朋友可以參考下
    2021-07-07
  • Vue transition過渡組件詳解

    Vue transition過渡組件詳解

    我們現在可以了解一下vue的過渡,vue在插入、更新以及移除DOM元素的時候,提供了很多不同方式過渡的效果,如果在css過渡自動應用class,在過渡鉤子函數中使用JavaScript直接操作DOM就可以了
    2022-08-08
  • vue項目如何引入json數據

    vue項目如何引入json數據

    這篇文章主要介紹了vue項目如何引入json數據,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue使用el-table實現表格跨頁多選

    Vue使用el-table實現表格跨頁多選

    在我們日常項目開發(fā)中,經常會有表格跨頁多選的需求,接下來讓我們用?el-table示例一步步來實現這個需求,文中有詳細的代碼講解,對我們的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-08-08
  • vue.js獲取數據庫數據實例代碼

    vue.js獲取數據庫數據實例代碼

    本篇文章主要介紹了vue.js獲取數據庫數據實例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • vue項目keepAlive配合vuex動態(tài)設置路由緩存方式

    vue項目keepAlive配合vuex動態(tài)設置路由緩存方式

    vue項目keepAlive配合vuex動態(tài)設置路由緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue完整項目構建(進階篇)

    Vue完整項目構建(進階篇)

    這篇文章主要介紹了Vue完整項目構建(進階篇)的相關資料,需要的朋友可以參考下
    2018-02-02
  • Vue引入高德地圖并觸發(fā)實現多個標點的示例詳解

    Vue引入高德地圖并觸發(fā)實現多個標點的示例詳解

    這篇文章主要介紹了Vue引入高德地圖并觸發(fā)實現多個標點,主要是在public下的index.html中引入地圖,引入組件設置寬高100%,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • vue項目使用electron進行打包操作的全過程

    vue項目使用electron進行打包操作的全過程

    我們都知道Electron項目分為了主進程和渲染進程,主進程其實就是我們的Electron,渲染進程就相當于我們的Vue項目,下面這篇文章主要給大家介紹了關于vue項目使用electron進行打包操作的全過程,需要的朋友可以參考下
    2023-03-03
  • websocket+Vuex實現一個實時聊天軟件

    websocket+Vuex實現一個實時聊天軟件

    這篇文章主要利用websocked 建立長連接,利用Vuex全局通信的特性,以及watch,computed函數監(jiān)聽消息變化,并驅動頁面變化實現實時聊天,感興趣的可以了解一下
    2021-08-08

最新評論