title: Switch

sidebar_label: Switch

开关选择器

支持情况:Switch - 图1 Switch - 图2 Switch - 图3 Switch - 图4 Switch - 图5 Switch - 图6 Switch - 图7

参考文档

类型

  1. ComponentType<SwitchProps>

示例代码

import Tabs from ‘@theme/Tabs’ import TabItem from ‘@theme/TabItem’

tsx export default class PageView extends Component { constructor() { super(...arguments) } render() { return ( <View className='components-page'> <Text>默认样式</Text> <Switch checked/> <Switch/> <Text>推荐展示样式</Text> <Switch checked/> <Switch/> </View> ) } } html <template> <view class='components-page'> <text>默认样式</text> <switch :checked="true" /> <switch /> <text>推荐展示样式</text> <switch :checked="true" /> <switch /> </view> </template>

SwitchProps

参数 类型 默认值 必填 说明
checked boolean false 是否选中
disabled boolean false 是否禁用
type “switch” or “checkbox” "switch" 样式,有效值:switch, checkbox
color string "#04BE02" switch 的颜色,同 css 的 color
onChange CommonEventFunction<onChangeEventDetail> checked 改变时触发 change 事件
nativeProps Record<string, unknown> 用于透传 WebComponents 上的属性到内部 H5 标签上

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 H5 React Native Harmony
SwitchProps.checked ✔️ ✔️ ✔️ ✔️ ✔️ ✔️
SwitchProps.disabled ✔️ ✔️
SwitchProps.type ✔️ ✔️ ✔️ ✔️ ✔️ ✔️
SwitchProps.color ✔️ ✔️ ✔️ ✔️ ✔️ ✔️
SwitchProps.onChange ✔️ ✔️
SwitchProps.nativeProps ✔️

onChangeEventDetail

参数 类型
value boolean