在鸿蒙平台上使用uni-app x开发应用时,switch开关组件是设置页面、表单中常见的交互元素。鸿蒙平台从4.61版本开始支持switch组件,但使用过程中存在属性废弃、平台差异等需要注意的问题。本文基于实际开发经验,梳理switch组件的属性、事件、实战场景以及鸿蒙平台的适配要点和踩坑记录。
一、switch组件基础
switch就是开/关的滑动开关。它与checkbox的区别在于:checkbox是勾选框,switch是滑动开关。功能上都是二选一,但交互形式不同。switch更适合设置页面,checkbox更适合表单选择。
基本用法如下:
- <template>
- <switch :checked="true" @change="onChange"></switch>
- </template>
- <script setup lang="uts">
- const onChange = (event: UniSwitchChangeEvent) => {
- console.log('开关状态:', event.detail.value) // true 或 false
- }
- </script>
复制代码
二、属性与事件详解
核心属性方面,type="checkbox"只有Web和微信小程序支持,App不支持。如果需要checkbox样式,直接用checkbox组件。
样式属性方面,color已废弃,用foreColor替代。这个变更跟radio、checkbox一样。activeBackgroundColor设置开启时的背景颜色,foreColor设置滑块颜色,backgroundColor设置背景颜色,activeForeColor设置开启时滑块颜色。
事件方面,change事件的detail.value是boolean类型,不是string。
三、实战场景
场景一:基础开关
- <template>
- <view style="padding: 15px;">
- <view style="flex-direction: row; align-items: center; justify-content: space-between;">
- <text style="font-size: 15px;">推送通知</text>
- <switch :checked="notifyEnabled" @change="onNotifyChange"></switch>
- </view>
- <text style="font-size: 12px; color: #999; margin-top: 5px;">状态:{{notifyEnabled ? '已开启' : '已关闭'}}</text>
- </view>
- </template>
- <script setup lang="uts">
- const notifyEnabled = ref(true)
- const onNotifyChange = (event: UniSwitchChangeEvent) => {
- notifyEnabled.value = event.detail.value
- }
- </script>
复制代码
场景二:设置页面
设置页面通常左侧文字说明,右侧放置开关。
- <template>
- <view style="background-color: white;">
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 15px; border-bottom: 1px solid #f0f0f0;">
- <view>
- <text style="font-size: 15px;">推送通知</text>
- <text style="font-size: 12px; color: #999;">接收新消息推送</text>
- </view>
- <switch :checked="notifyEnabled" @change="onNotifyChange"></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 15px; border-bottom: 1px solid #f0f0f0;">
- <view>
- <text style="font-size: 15px;">深色模式</text>
- <text style="font-size: 12px; color: #999;">跟随系统或手动切换</text>
- </view>
- <switch :checked="darkMode" @change="onDarkModeChange"></switch>
- </view>
- </view>
- </template>
- <script setup lang="uts">
- const notifyEnabled = ref(true)
- const darkMode = ref(false)
- const onNotifyChange = (event: UniSwitchChangeEvent) => {
- notifyEnabled.value = event.detail.value
- }
- const onDarkModeChange = (event: UniSwitchChangeEvent) => {
- darkMode.value = event.detail.value
- }
- </script>
复制代码
场景三:禁用状态
禁用后开关不可点击。
- <template>
- <view style="padding: 15px;">
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px;">可操作</text>
- <switch :checked="true"></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px; color: #999;">禁用(开启状态)</text>
- <switch :checked="true" disabled></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px; color: #999;">禁用(关闭状态)</text>
- <switch :checked="false" disabled></switch>
- </view>
- </view>
- </template>
复制代码
场景四:自定义颜色
- <template>
- <view style="padding: 15px;">
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px;">绿色开关</text>
- <switch :checked="true" activeBackgroundColor="#4CAF50" foreColor="#ffffff"></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px;">橙色开关</text>
- <switch :checked="true" activeBackgroundColor="#FF9800" foreColor="#ffffff"></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
- <text style="font-size: 15px;">暗黑模式</text>
- <switch :checked="true" backgroundColor="#1f1f1f" activeBackgroundColor="#007aff" foreColor="#f0f0f0" activeForeColor="#ffffff"></switch>
- </view>
- </view>
- </template>
复制代码
场景五:配合form使用
switch的name属性会作为表单提交的key,checked状态作为值(true/false)。
- <template>
- <form @submit="onSubmit">
- <view style="padding: 15px;">
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #f0f0f0;">
- <text style="font-size: 15px;">推送通知</text>
- <switch name="notify" :checked="true"></switch>
- </view>
- <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #f0f0f0;">
- <text style="font-size: 15px;">深色模式</text>
- <switch name="darkMode" :checked="false"></switch>
- </view>
- <button form-type="submit" type="primary" style="margin-top: 20px;">保存设置</button>
- </view>
- </form>
- </template>
- <script setup lang="uts">
- const onSubmit = (event: UniFormSubmitEvent) => {
- console.log('设置:', event.detail.value)
- // { notify: true, darkMode: false, autoSave: true }
- }
- </script>
复制代码
四、鸿蒙平台专属特性
鸿蒙平台兼容性:HarmonyOS 4.61版本开始支持switch组件。
鸿蒙平台原生实现:switch在鸿蒙平台上使用原生实现,对应鸿蒙的Toggle组件。原生实现的好处是滑动流畅、系统风格一致。在鸿蒙原生开发中,开关使用Toggle组件:
- // 鸿蒙原生写法
- Toggle({ type: ToggleType.Switch, isOn: true })
- .onChange((isOn: boolean) => {
- console.log('开关状态:', isOn)
- })
复制代码
对比下来,uni-app x的switch写法更简洁,属性名也更直观。
鸿蒙平台样式差异:鸿蒙平台的switch默认样式可能跟其他平台有差异,默认大小可能不同,动画效果可能有差异,颜色可能不完全一致。建议显式设置颜色属性,确保各平台效果一致。
padding支持:鸿蒙平台支持padding,但iOS平台不支持switch的padding style。如果需要padding,建议在外层view上设置。
五、踩坑记录
坑一:color属性已废弃。用foreColor替代。如果你还在用color,可能会有警告。
- <!-- 正确 -->
- <switch foreColor="#ffffff"></switch>
- <!-- 废弃 -->
- <switch color="#ffffff"></switch>
复制代码
坑二:type="checkbox"不支持App。type="checkbox"只有Web和微信小程序支持,App不支持。如果需要checkbox样式,直接用checkbox组件。
坑三:change事件的value是boolean。switch的change事件detail.value是boolean类型,不是string。别跟radio搞混了,radio的value是string。
- const onChange = (event: UniSwitchChangeEvent) => {
- // 正确
- const isChecked: boolean = event.detail.value
- // 错误:以为是string
- // const value: string = event.detail.value
- }
复制代码
六、总结
在鸿蒙平台使用uni-app x的switch组件时,注意属性更新、平台差异和事件类型。显式设置颜色属性,避免使用废弃属性,正确处理boolean值。结合原生Toggle组件的特性,可以更好地实现流畅的开关交互。 |