uView Button 按钮

该组件内部实现以uni-appbutton组件为基础,进行二次封装,主要区别在于:

  • 按钮type值有更多的主题颜色
  • 按钮size值有更多的尺寸可选

注意

  1. 此组件内部使用uni-appbutton组件为基础,除了开头中所说的增加的功能,另外暴露出来的props属性和官方组件的属性完全一致, uni-appbutton组件比较特殊,因为它有一些其他小程序平台的特定能力,请参考文档后面的参数列表,更详细说明请参uni-app方文档:
    uni-app方button组件(opens new window)
  2. 由于微信小程序的限制,在微信小程序中设置了form-typeu-button无法触发form组件的submit事件(H5和APP正常),详见微信小程序文档Bug & Tip部分(opens new window)

#平台差异说明

App(vue) App(nvue) H5 小程序

#基本使用

文字内容通过text传入

复制代码
<u-button text="月落"></u-button>

copy

#设置按钮的多种形态

  • type值可选的有default(默认)、primarysuccessinfowarningerror

  • 通过plain值设置是否镂空

  • 通过hairline值设置是否细边

  • 通过disabled值设置是否禁用

  • 通过loading值设置是否开启加载图标,loadingText设置加载中文字

  • 通过icon值设置是否显示图标

  • 通过shape值设置按钮形状,circle为圆角

  • 通过color值设置按钮渐变颜色

  • 通过size值设置按钮的大小

    <template> <view style="padding: 20px;"> <u-button type="primary" text="确定"></u-button> <u-button type="primary" :plain="true" text="镂空"></u-button> <u-button type="primary" :plain="true" :hairline="true" text="细边"></u-button> <u-button type="primary" :disabled="disabled" text="禁用"></u-button> <u-button type="primary" loading loadingText="加载中"></u-button> <u-button type="primary" icon="map" text="图标按钮"></u-button> <u-button type="primary" shape="circle" text="按钮形状"></u-button> <u-button text="渐变色按钮" color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))"></u-button> <u-button type="primary" size="small" text="大小尺寸"></u-button> </view> </template> <script> export default { data() { return { disabled: true }; } }; </script>

copy

#定义需要用到的外部样式

  1. 针对非微信小程序平台,组件的根元素就是uni-appbutton组件,所以修改按钮的样式很容易,直接给组件定义类名或者嵌入内联样式即可。
  2. 如果是微信小程序,编译后页面会有组件同名的元素存在,导致样式传递有问题。
  3. 如果是为了修改按钮与其他元素之间的距离或者宽度等,可以给按钮外面套一个view元素,控制这个view与其他元素的距离或者宽度,即可达到同等效果。

所以:我们提供了一个custom-style参数,推荐用户可以用对象形式传递样式给组件内部,注意驼峰命名。

复制代码
<template>
	<view style="padding: 20px;">
		 <!-- 以下形式在微信小程序会无效,APP和H5有效  -->
		<u-button class="custom-style" text="雪月夜"></u-button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			disabled: true,
			customStyle: {
				marginTop: '20px', // 注意驼峰命名,并且值必须用引号包括,因为这是对象
				color: 'red'
			}
		};
	}
};
</script>

<style lang="scss" scoped>
	.custom-style {
		color: #ff0000;
		width: 400rpx;
	}
</style>
相关推荐
东东51612 小时前
xxx食堂移动预约点餐系统 (springboot+微信小程序)
spring boot·微信小程序·小程序·毕业设计·个人开发·毕设
韩立学长18 小时前
【开题答辩实录分享】以《智慧校园平台微信小程序》为例进行选题答辩实录分享
spring boot·微信小程序·小程序
h_654321018 小时前
微信小程序:按顺序一张图片加载完后,再加载另一张
微信小程序·小程序
码农阿豪1 天前
轻断食14+7方案小程序项目 - 完整实现方案
小程序·notepad++
qq_316837752 天前
uniapp打包微信小程序使用插件
微信小程序·小程序·uni-app
不爱学习小趴菜3 天前
uniapp微信小程序无法屏蔽右上角胶囊按钮(...)问题解决方案
微信小程序·小程序·uni-app
plmm烟酒僧3 天前
《微信小程序demo开发》第一部分-编写页面逻辑
javascript·微信小程序·小程序·html·微信开发者工具·小程序开发
Stara05113 天前
微信小程序配置与导航深度指南—全局/页面配置解析与高效传参策略V1.2
javascript·微信小程序·html·模块化开发·页面导航·wxs脚本·小程序api
全栈小53 天前
【小程序】微信小程序input设置readonly只读属性无效,这是怎么回事呢
微信小程序·小程序·notepad++
ruxshui4 天前
Notepad++ 正则匹配实战:时间戳及高频场景
linux·服务器·notepad++