vue3自定义svg组件封装后图标很小修复

文章讨论了在Vue项目中,使用SVG图标时遇到的问题,发现SVG图标在grid组件开启square模式下会变得迷你,解决方案是移除square限制。开发者分享了代码示例和解决方法。
摘要由CSDN通过智能技术生成

问题描述:

如下图,同样的图标,没有额外增加样式,就非常mini

在这里插入图片描述
而且svg标签的大小就是我style设置的
在这里插入图片描述

但是,这是咋回事呀,真的很不理解
在这里插入图片描述

下面是我出现问题的代码

SvgIcon.vue👇 这是components

<template>
    <svg :width="width" :height="height" class="svg-icon">
      <use :href="iconName" />
    </svg>
  </template>
   
<script setup>
    import { defineProps, computed } from 'vue';

    const props = defineProps({
        name: {
            type: String,
            required: true,
        },
        color: {
            type: String,
            default: '#333',
        },
        width: {
            type: String,
            default: '24px',
        },
        height: {
            type: String,
            default: '24px',
        },
    })
    const iconName = computed(()=> `#${props.name}`)
    const svgClass = computed(()=>{
        if(props.name){
            return `svg-icon ${iconName.value}`
        }
        return "svg-icon"
    })
</script><style scoped>
    .svg-icon {
        width: 24px;
        height: 24px;
        fill: currentColor;
        vertical-align: middle;
    }
</style>

一个单独加了样式的就是正常大小,另外默认组件样式的就是那么的mini
index.vue👇

<template>
    <div class="home-main">
        <van-grid :column-num="5" :border="false" class="home-navs-top">
            <van-grid-item v-for="(item,i) in home_navs_top" :key="i" :to="item.toUrl" >
                <svg-icon :name="item.icon" class="navs-top-svg" />
                <div>{{ item.text }}</div>
            </van-grid-item>
        </van-grid>
        <!-- 中间多行nav -->
        <van-grid square :column-num="5" :border="false" class="home-navs">
            <van-grid-item v-for="(item,i) in home_navs" :key="i" :to="item.toUrl" >
                <svg-icon :name="item.icon" class="icon-svg" />
                <div class="home-navs_text">{{ item.text }}</div>
            </van-grid-item>
        </van-grid>

    </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
const home_navs_top = ref([
    {icon:'food',text:'美食',toUrl:'',},
    {icon:'food',text:'甜点饮品',toUrl:'',},
    {icon:'food',text:'超市便利',toUrl:'',},
    {icon:'food',text:'生鲜果蔬',toUrl:'',},
    {icon:'food',text:'外卖买药',toUrl:'',},
]);
const home_navs = ref([
    {icon:'food',text:'午餐',toUrl:'',},
    {icon:'food',text:'买酒',toUrl:'',},
    {icon:'food',text:'新鲜水果',toUrl:'',},
    {icon:'food',text:'汉堡披萨',toUrl:'',},
]);
</script>


<style lang="scss" scoped>
.home-main{
    background: #fff;
    margin-top: -30px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    overflow: hidden;
    .home-navs-top{
        .navs-top-svg{
            width: 2.8rem;
            height: 2.8rem;
        }
    }
    .home-navs{
        margin-top: 26px;
        .home-navs_text{
            margin-top: 12px;
        }
    }
}
</style>

vite.config.ts👇

import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';


// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
      vue(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
      createSvgIconsPlugin({
          // 指定需要缓存的图标文件夹
          iconDirs: [path.resolve(process.cwd(), 'src/icon/svg')],
          // 指定symbolId格式
          symbolId: '[name]',
      })
  ],
  resolve:{
    alias:{
      '@':path.resolve(process.cwd(),'src'),
      '~':path.resolve(process.cwd(),'./'),
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "./src/base.scss";`
      }
    }
  }
})

main.ts👇

import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)

// router路由
import router from './router'
app.use(router)

// vant
import 'vant/lib/index.css';

// store
import {store,key} from './store'
app.use(store,key)

// 导入svg图片插件,可以在页面上显示svg图片
import 'virtual:svg-icons-register'
import SvgIcon from './components/SvgIcon.vue'
app.component('svg-icon', SvgIcon)


app.mount('#app')

解决办法

真的是破案了破案了,我用的grid开了 squrae 限制了高宽
把‘squrae ’去掉就完事了

在这里插入图片描述

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中的自定义指令封装是一种扩展Vue的能力,允许开发者在DOM元素上添加自定义行为。下面是Vue 3中自定义指令封装的步骤: 1. 创建自定义指令:使用`app.directive`方法来创建自定义指令。该方法接受两个参数,第一个参数是指令名称,第二个参数是一个对象,包含了指令的各种钩子函数和配置选项。 2. 钩子函数:自定义指令可以通过钩子函数来定义其行为。常用的钩子函数有: - `beforeMount`:在指令绑定的元素挂载到DOM之前调用。 - `mounted`:在指令绑定的元素挂载到DOM之后调用。 - `beforeUpdate`:在指令所在组件更新之前调用。 - `updated`:在指令所在组件更新之后调用。 - `beforeUnmount`:在指令所在组件卸载之前调用。 - `unmounted`:在指令所在组件卸载之后调用。 3. 配置选项:除了钩子函数外,还可以通过配置选项来定义自定义指令的行为。常用的配置选项有: - `bind`:在指令绑定到元素时立即调用,只调用一次。 - `update`:在指令所在组件的VNode更新时调用,可能调用多次。 - `unbind`:在指令从元素上解绑时调用,只调用一次。 下面是一个示例,演示了如何在Vue 3中封装一个自定义指令: ```javascript // 创建Vue实例 const app = Vue.createApp({}); // 创建自定义指令 app.directive('my-directive', { beforeMount(el, binding, vnode) { // 指令绑定到元素之前的操作 }, mounted(el, binding, vnode) { // 指令绑定到元素之后的操作 }, beforeUpdate(el, binding, vnode) { // 指令所在组件更新之前的操作 }, updated(el, binding, vnode) { // 指令所在组件更新之后的操作 }, beforeUnmount(el, binding, vnode) { // 指令所在组件卸载之前的操作 }, unmounted(el, binding, vnode) { // 指令所在组件卸载之后的操作 } }); // 将Vue实例挂载到DOM元素上 app.mount('#app'); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值