用vue组件实现不同样式的按钮

说明

在element的按钮组件中,我们可以看到如下所示的按钮:
在这里插入图片描述
接下来,我将用vue中的组件来实现它。

如何实现

首先我们将不用类名的样式写好,然后通过type给按钮传入对应的类名,最后通过组件传值,将类名绑定在当前组件上。

代码

直接上代码了:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
		<style>
			.button {
				border: 1px solid #ccc;
				border-radius: 10px;
				width: 100px;
				height: 40px;
				color: white;
			}
			.button.button-default {
				background: #fff;
				color: black;
			}
			.button.button-primary {
				background: #409eff;
			}
			.button.button-success {
				background: #67c23a;
			}
			.button.button-infomation {
				background: #909399;
			}
			.button.button-warning {
				background: #e6c23a;
			}
			.button.button-danger {
				background: #f56c6c;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<el-button type="default">默认按钮</el-button>
			<el-button type="primary">主要按钮</el-button>
			<el-button type="success">成功按钮</el-button>
			<el-button type="infomation">信息按钮</el-button>
			<el-button type="warning">警告按钮</el-button>
			<el-button type="danger">危险按钮</el-button>
		</div>
		<template id="button">
			<button :class="['button','button-'+ type]">
				<slot></slot>
			</button>
		</template>
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<script>
			Vue.component("el-button", {
				template: "#button",
				props: ["type"],
			});
			new Vue({
				el: "#app",
			});
		</script>
	</body>
</html>

代码运行结果如下图所示:

在这里插入图片描述

结语

以上就是不同样式按钮的实现方式,希望对您能有帮助。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值