Vue3封装Butoon组件

本文介绍了如何在Vue3项目中创建一个可复用的按钮组件,支持动态样式(包括SVG图标),并展示了如何在模板和样式中进行封装和使用。
摘要由CSDN通过智能技术生成

因为在项目中会经常用到按钮,基于vue的组件化开发,我就想封装一个button组件,供全局使用,本文实现的是简单的封装,涉及到更多要求的也可以按照这个模版继续添加

工具:

  1. vue3
  2. svg

如果不想在按钮里加图标,可以用svg或者其他图标框架,总之能实现在按钮里加上图标即可,本文使用的是svg作为icon引入,如果也想使用svg,请看:Vue3+Vite 全局封装Svg图标-CSDN博客

1. 封装

<script lang="ts" setup>
interface Props {
    width?: number, // 按钮宽度
    height?: number, // 按钮高度
    borderRadius?: number, // 按钮圆角
    bg?: string, // 背景颜色
    showIcon?: boolean, // 是否显示icon
    iconName?: string, // icon名称
    text: string, // 按钮文字
}

const props = withDefaults(defineProps<Props>(), {
    width: 100, // 默认宽度100
    height: 35, // 默认高度35
    borderRadius: 20, // 默认圆角度数:20
    showIcon: false, // 默认不显示icon
})
// 直接在js中处理拼接好样式,然后通过vue的样式绑定到按钮上就可以实现按钮样式动态改变了
// 按钮的样式
const btnStyle = {
    width: props.width + 'px',
    height: props.height + 'px',
    borderRadius: props.borderRadius + 'px',
    fontSize: (props.height - 20) + 'px',
}
// 图标样式
const iconStyle = {
    width: (props.height - 15) + 'px',
    height: (props.height - 15) + 'px',
}
</script>

<template>
    <div class="btn-container">
        <button :style="btnStyle">
            <svg v-if="showIcon" class="icon" :style="iconStyle">
                <use :xlink:href="`#icon-${iconName}`"/>
            </svg>
            {{ text }}
        </button>
    </div>
</template>

<style lang="scss" scoped>
.btn-container {
  button {
    border: 1px solid #eee;
    background-color: #fff;
    color: #000;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;

    .icon {
      margin-right: 5px;
    }
  }

  button:hover {
    cursor: pointer;
  }
}
</style>

2. 使用

<MyButton :text="'按钮'" :iconName="'location'" :show-icon="true"/>

3. 效果

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于Vue3封装ECharts组件的方法,我可以给你一些提示。 首先,我们需要安装ECharts。在Vue项目中,可以使用npm或yarn来安装ECharts。 ```bash npm install echarts --save # 或者 yarn add echarts ``` 现在,我们就可以在Vue3项目中使用ECharts了。 接下来,我们需要创建一个封装ECharts的Vue组件。我们可以使用Vue3中的`<script setup>`语法来创建组件。 ```vue <template> <div ref="chart" :style="{ width: width, height: height }"></div> </template> <script setup> import * as echarts from 'echarts'; const props = defineProps({ option: { type: Object, required: true, }, width: { type: String, default: '100%', }, height: { type: String, default: '400px', }, }); const chart = ref(null); onMounted(() => { const echartsInstance = echarts.init(chart.value); echartsInstance.setOption(props.option); window.addEventListener('resize', () => { echartsInstance.resize(); }); }); onUnmounted(() => { window.removeEventListener('resize', () => {}); }); </script> ``` 在这个组件中,我们使用了`<div>`元素来包含ECharts图表,并使用`ref`属性来获取DOM元素。我们还通过`props`定义了一些参数,包括ECharts的配置参数(`option`)、组件的宽度(`width`)和高度(`height`)。 在组件的`onMounted`生命周期钩子函数中,我们使用ECharts的`init`方法来创建一个ECharts实例,并将图表的配置参数传递给`setOption`方法。我们还添加了一个`resize`事件监听器,以便在窗口大小变化时自动调整图表的大小。 最后,在组件的`onUnmounted`生命周期钩子函数中,我们移除了`resize`事件监听器,以避免出现内存泄漏。 这就是一个简单的Vue3封装ECharts组件的示例。你可以在父组件中使用这个组件,并通过`option`属性传递ECharts的配置参数。 ```vue <template> <ECharts :option="chartOption" /> </template> <script> import ECharts from './ECharts.vue'; export default { components: { ECharts, }, data() { return { chartOption: { // ECharts配置参数 }, }; }, }; </script> ``` 希望这些提示能够帮到你!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值