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

vue3+vue-cli4中使用svg的方式詳解(親測(cè)可用)

 更新時(shí)間:2022年08月09日 15:28:28   作者:zasulan  
最近在做個(gè)vue的項(xiàng)目,從各種github上的開源庫(kù)上借鑒開發(fā)方法,給大家分享下,這篇文章主要給大家介紹了關(guān)于vue3+vue-cli4中使用svg的相關(guān)資料,需要的朋友可以參考下

技術(shù)棧:vue3+vue-cli4

前言:

目前大多數(shù)是基于vue2引入,所以想基于vue3需要做一些改動(dòng)(注意該方法是基于vue-cli中使用的,因?yàn)閣ebpack提供了require.context(),基于vite構(gòu)建的項(xiàng)目則不能使用該文章提供的方法)

一、安裝 svg-sprite-loader

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

二、在src/components/svgIcon下新建組件index.vue

<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName" rel="external nofollow" ></use>
  </svg>
</template>

<script>
import { computed } from "@vue/reactivity";
export default {
  name: "baseSvgIcon",
  props: {
    iconClass: { type: String },
    className: { type: String },
  },
  setup(props) {
    const iconName = computed(() => {
      return props.iconClass ? `#icon-${props.iconClass}` : "#icon";
    });
    const svgClass = computed(() => {
      return props.className ? "svg-icon " + props.className : "svg-icon";
    });
    return { iconName, svgClass };
  },
};
</script>

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

三、在assets在創(chuàng)建icons文件夾

imgs文件夾里含一個(gè)svg文件夾,放svg格式文件,以及一個(gè)index.js文件,該文件內(nèi)容如下

// 獲取當(dāng)前目錄所有為.svg的文件
const req = require.context('./svg', false, /\.svg$/)

// 解析獲取的.svg文件的文件名稱并返回
const requireAll = (requireContext) =>{
    return requireContext.keys().map(requireContext)
}
requireAll(req)

四、在src同級(jí)下創(chuàng)建vue.config.js進(jìn)行配置

經(jīng)評(píng)論區(qū)總結(jié),如果是在非vue-cli4的項(xiàng)目中,在config.module.rules.delete("svg");報(bào)錯(cuò)的話,可以嘗試使用config.module.rule("svg").exclude.add(resolve("src/assets/imgs")).end();替換該語(yǔ)句

const path = require('path')

function resolve(dir) {
  return path.join(__dirname, '.', dir)
}

module.exports = {
  chainWebpack: config => {
    config.module.rules.delete("svg"); // 重點(diǎn):刪除默認(rèn)配置中處理svg,
    config.module
      .rule('svg-sprite-loader')
      .test(/\.svg$/)
      .include
      .add(resolve('src/assets/imgs')) // 處理svg目錄
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
  },
};

五、在main.js里引入,以及做一些小修改

此處注意,將組件注冊(cè)放到main.js里。不然會(huì)報(bào)[Vue warn]: Failed to resolve component: svg-icon的問(wèn)題,預(yù)測(cè)為父組件先創(chuàng)建完了而子組件還沒(méi)創(chuàng)建進(jìn)去,導(dǎo)致該問(wèn)題的出現(xiàn)

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import '@/assets/icons'

const app = createApp(App)

import SvgIcon from '@/components/svgIcon' 
app.component('svg-icon', SvgIcon)

app.use(store).use(router).mount('#app')

六、在頁(yè)面中使用

<div class="topLeft">
  <svg-icon icon-class="category"></svg-icon>
</div>
<div class="topCenter"></div>
<div class="topRight">
  <svg-icon icon-class="search"></svg-icon>
</div>

七、文件目錄結(jié)構(gòu)及其效果展示

八、參考鏈接地址

1、如果是vue2進(jìn)行使用的可以參考該文章:https://www.jianshu.com/p/cb67bb4a79f2

2、我看到過(guò)的另一種解決思路,將index.js文件里的內(nèi)容放入到main.js里,但我覺(jué)得分開層次更清晰,該解決方案在該文章的評(píng)論里:https://zhuanlan.zhihu.com/p/335107384

3、在vite中使用svg的可以參考我另一篇文章:http://www.dbjr.com.cn/article/258650.htm

4、評(píng)論區(qū)里有人成功了,我上傳一版demo放到gitee里了,有問(wèn)題的地方大家可以對(duì)照看看:https://gitee.com/zasulan/csdn-item/tree/master/vue-music-app,重新上傳了一版無(wú)node-scss的版本,因?yàn)榇蠹业膎ode版本或者vue-cli版本可能不一致,npm install會(huì)報(bào)錯(cuò)因此去除,有需要可自行安裝

總結(jié)

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

相關(guān)文章

最新評(píng)論