Skip to content
On this page

组件库文档 tmui.design

开关 Switch

Switch 开关是用于在两种状态下选择唯一一种状态或值的组件


🌶️ 开关 Switch 示例

查看模拟效果
示例模板
vue
<template>
	<tm-app>
		<tm-sheet>
			<tm-text :font-size="24" _class="font-weight-b" label="基础示例,更多见文档"></tm-text>
			<tm-divider></tm-divider>
			<view class="flex flex-row">
				<tm-switch size="mini"></tm-switch>
				<tm-switch  size="normal" color="red" :defaultValue="true" :margin="[12, 0]"></tm-switch>
				<tm-switch size="large" color="green" :defaultValue="true" :margin="[12, 0]"></tm-switch>
			</view>
		</tm-sheet>
		<tm-sheet>
			<tm-text :font-size="24" _class="font-weight-b" label="其它样式属性"></tm-text>
			<tm-divider></tm-divider>
			<view class="flex flex-row">
				<tm-switch  linear="left" linearDeep="accent" :defaultValue="true" :margin="[12, 0]"></tm-switch>
				<tm-switch unCheckedColor="red" color="red" text :label="['开','关']"></tm-switch>
				<tm-switch :label="['形状','方形']" :width="200" :height="58" :round="2" color="green" :defaultValue="true" :margin="[12, 0]"></tm-switch>
			</view>
		</tm-sheet>
		<tm-sheet>
			<tm-text :font-size="24" _class="font-weight-b" label="加载和禁用"></tm-text>
			<tm-divider></tm-divider>
			<view class="flex flex-row">
				<tm-switch load  linear="left" linearDeep="accent" color="red" :defaultValue="true" :margin="[12, 0]"></tm-switch>
				<tm-switch disabled  linear="left" linearDeep="accent" color="green" :defaultValue="true" :margin="[12, 0]"></tm-switch>
			</view>
		</tm-sheet>
		<tm-sheet>
			<tm-text :font-size="24" _class="font-weight-b" label="异步开关"></tm-text>
			<tm-divider></tm-divider>
			<view class="flex flex-row">
				<tm-switch :beforeChecked="beforeChecked"  linear="left" linearDeep="accent" color="blue" :margin="[12, 0]"></tm-switch>
			</view>
		</tm-sheet>
	</tm-app>
</template>
<script lang="ts" setup>
import { ref, computed } from "vue"
import { onShow, onLoad } from "@dcloudio/uni-app";
import tmApp from "@/tmui/components/tm-app/tm-app.vue"
import tmSheet from "@/tmui/components/tm-sheet/tm-sheet.vue"
import tmText from "@/tmui/components/tm-text/tm-text.vue"
import tmSwitch from "@/tmui/components/tm-switch/tm-switch.vue"
import tmDivider from "@/tmui/components/tm-divider/tm-divider.vue"
function beforeChecked(){
	return new Promise(res=>{
		setTimeout(function() {
			res(true)
		}, 1000);
	})
}


</script>

🌶️ 兼容性

APP-VUEAPP-NVUE小程序WEB/H5VUE3/TS
✔️✔️✔️✔️✔️

🌱 参数

本组件含有公共属性 公共属性

参数名类型默认值描述
widthNumber0组件宽度
heightNumber0组件高度
sizeStringnormal组件大小,可取的值有:mini,normal,large
marginArray[0,0]组件外边距
transprentBooleanfalse组件是否透明
modelValue / v-modelBooleanfalse组件的值,可双向绑定
defaultValueBooleanfalse组件默认值
selectedv3.0.83+Boolean,String,Numbertrue打开时的值
unSelectedv3.0.83+Boolean,String,Numberfalse关闭时的值
colorStringprimary组件激活后的颜色,即按钮开启状态的颜色
unCheckedColorString组件未激活的颜色,即按钮关闭状态的颜色
barColorStringwhite开关上小圆球的背景色
roundNumber10组件的圆角值
loadBooleanfalse是否显示组件加载状态,加载状态下组件圆形内按钮会变成旋转的加载图标
beforeCheckedFunction / String / Numberfalse开关状态改变前执行的函数
barIconStringtmicon-check开关按钮上圆形内的图标
disabledBooleanfalse是否禁用组件
labelArray["",""]开关开启关闭时内部的文字,如:["开启","关闭"]

🌹 事件

事件名参数返回数据描述
change/modelValue开关状态改变时返回当前的状态
click//开关被点击后的函数

🌽 slot插槽

🥗 ref方法

😡 贡献者

Wicos