查看: 255|回复: 0

鸿蒙uni-app x开关switch组件开发与避坑指南

[复制链接]
发表于 3 小时前 | 显示全部楼层 |阅读模式
在鸿蒙平台上使用uni-app x开发应用时,switch开关组件是设置页面、表单中常见的交互元素。鸿蒙平台从4.61版本开始支持switch组件,但使用过程中存在属性废弃、平台差异等需要注意的问题。本文基于实际开发经验,梳理switch组件的属性、事件、实战场景以及鸿蒙平台的适配要点和踩坑记录。

一、switch组件基础

switch就是开/关的滑动开关。它与checkbox的区别在于:checkbox是勾选框,switch是滑动开关。功能上都是二选一,但交互形式不同。switch更适合设置页面,checkbox更适合表单选择。

基本用法如下:
  1. <template>
  2.   <switch :checked="true" @change="onChange"></switch>
  3. </template>
  4. <script setup lang="uts">
  5. const onChange = (event: UniSwitchChangeEvent) => {
  6.   console.log('开关状态:', event.detail.value) // true 或 false
  7. }
  8. </script>
复制代码

二、属性与事件详解

核心属性方面,type="checkbox"只有Web和微信小程序支持,App不支持。如果需要checkbox样式,直接用checkbox组件。

样式属性方面,color已废弃,用foreColor替代。这个变更跟radio、checkbox一样。activeBackgroundColor设置开启时的背景颜色,foreColor设置滑块颜色,backgroundColor设置背景颜色,activeForeColor设置开启时滑块颜色。

事件方面,change事件的detail.value是boolean类型,不是string。

三、实战场景

场景一:基础开关
  1. <template>
  2.   <view style="padding: 15px;">
  3.     <view style="flex-direction: row; align-items: center; justify-content: space-between;">
  4.       <text style="font-size: 15px;">推送通知</text>
  5.       <switch :checked="notifyEnabled" @change="onNotifyChange"></switch>
  6.     </view>
  7.     <text style="font-size: 12px; color: #999; margin-top: 5px;">状态:{{notifyEnabled ? '已开启' : '已关闭'}}</text>
  8.   </view>
  9. </template>
  10. <script setup lang="uts">
  11. const notifyEnabled = ref(true)
  12. const onNotifyChange = (event: UniSwitchChangeEvent) => {
  13.   notifyEnabled.value = event.detail.value
  14. }
  15. </script>
复制代码

场景二:设置页面

设置页面通常左侧文字说明,右侧放置开关。
  1. <template>
  2.   <view style="background-color: white;">
  3.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 15px; border-bottom: 1px solid #f0f0f0;">
  4.       <view>
  5.         <text style="font-size: 15px;">推送通知</text>
  6.         <text style="font-size: 12px; color: #999;">接收新消息推送</text>
  7.       </view>
  8.       <switch :checked="notifyEnabled" @change="onNotifyChange"></switch>
  9.     </view>
  10.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 15px; border-bottom: 1px solid #f0f0f0;">
  11.       <view>
  12.         <text style="font-size: 15px;">深色模式</text>
  13.         <text style="font-size: 12px; color: #999;">跟随系统或手动切换</text>
  14.       </view>
  15.       <switch :checked="darkMode" @change="onDarkModeChange"></switch>
  16.     </view>
  17.   </view>
  18. </template>
  19. <script setup lang="uts">
  20. const notifyEnabled = ref(true)
  21. const darkMode = ref(false)
  22. const onNotifyChange = (event: UniSwitchChangeEvent) => {
  23.   notifyEnabled.value = event.detail.value
  24. }
  25. const onDarkModeChange = (event: UniSwitchChangeEvent) => {
  26.   darkMode.value = event.detail.value
  27. }
  28. </script>
复制代码

场景三:禁用状态

禁用后开关不可点击。
  1. <template>
  2.   <view style="padding: 15px;">
  3.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  4.       <text style="font-size: 15px;">可操作</text>
  5.       <switch :checked="true"></switch>
  6.     </view>
  7.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  8.       <text style="font-size: 15px; color: #999;">禁用(开启状态)</text>
  9.       <switch :checked="true" disabled></switch>
  10.     </view>
  11.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  12.       <text style="font-size: 15px; color: #999;">禁用(关闭状态)</text>
  13.       <switch :checked="false" disabled></switch>
  14.     </view>
  15.   </view>
  16. </template>
复制代码

场景四:自定义颜色
  1. <template>
  2.   <view style="padding: 15px;">
  3.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  4.       <text style="font-size: 15px;">绿色开关</text>
  5.       <switch :checked="true" activeBackgroundColor="#4CAF50" foreColor="#ffffff"></switch>
  6.     </view>
  7.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  8.       <text style="font-size: 15px;">橙色开关</text>
  9.       <switch :checked="true" activeBackgroundColor="#FF9800" foreColor="#ffffff"></switch>
  10.     </view>
  11.     <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0;">
  12.       <text style="font-size: 15px;">暗黑模式</text>
  13.       <switch :checked="true" backgroundColor="#1f1f1f" activeBackgroundColor="#007aff" foreColor="#f0f0f0" activeForeColor="#ffffff"></switch>
  14.     </view>
  15.   </view>
  16. </template>
复制代码

场景五:配合form使用

switch的name属性会作为表单提交的key,checked状态作为值(true/false)。
  1. <template>
  2.   <form @submit="onSubmit">
  3.     <view style="padding: 15px;">
  4.       <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #f0f0f0;">
  5.         <text style="font-size: 15px;">推送通知</text>
  6.         <switch name="notify" :checked="true"></switch>
  7.       </view>
  8.       <view style="flex-direction: row; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #f0f0f0;">
  9.         <text style="font-size: 15px;">深色模式</text>
  10.         <switch name="darkMode" :checked="false"></switch>
  11.       </view>
  12.       <button form-type="submit" type="primary" style="margin-top: 20px;">保存设置</button>
  13.     </view>
  14.   </form>
  15. </template>
  16. <script setup lang="uts">
  17. const onSubmit = (event: UniFormSubmitEvent) => {
  18.   console.log('设置:', event.detail.value)
  19.   // { notify: true, darkMode: false, autoSave: true }
  20. }
  21. </script>
复制代码

四、鸿蒙平台专属特性

鸿蒙平台兼容性:HarmonyOS 4.61版本开始支持switch组件。

鸿蒙平台原生实现:switch在鸿蒙平台上使用原生实现,对应鸿蒙的Toggle组件。原生实现的好处是滑动流畅、系统风格一致。在鸿蒙原生开发中,开关使用Toggle组件:
  1. // 鸿蒙原生写法
  2. Toggle({ type: ToggleType.Switch, isOn: true })
  3.   .onChange((isOn: boolean) => {
  4.     console.log('开关状态:', isOn)
  5.   })
复制代码

对比下来,uni-app x的switch写法更简洁,属性名也更直观。

鸿蒙平台样式差异:鸿蒙平台的switch默认样式可能跟其他平台有差异,默认大小可能不同,动画效果可能有差异,颜色可能不完全一致。建议显式设置颜色属性,确保各平台效果一致。

padding支持:鸿蒙平台支持padding,但iOS平台不支持switch的padding style。如果需要padding,建议在外层view上设置。

五、踩坑记录

坑一:color属性已废弃。用foreColor替代。如果你还在用color,可能会有警告。
  1. <!-- 正确 -->
  2. <switch foreColor="#ffffff"></switch>
  3. <!-- 废弃 -->
  4. <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。
  1. const onChange = (event: UniSwitchChangeEvent) => {
  2.   // 正确
  3.   const isChecked: boolean = event.detail.value
  4.   // 错误:以为是string
  5.   // const value: string = event.detail.value
  6. }
复制代码

六、总结

在鸿蒙平台使用uni-app x的switch组件时,注意属性更新、平台差异和事件类型。显式设置颜色属性,避免使用废弃属性,正确处理boolean值。结合原生Toggle组件的特性,可以更好地实现流畅的开关交互。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

指导单位

江苏省公安厅

江苏省通信管理局

浙江省台州刑侦支队

DEFCON GROUP 86025

Hacking Group 021A

旗下站点

态势感知中心

应急响应中心

红盟安全

联系我们

官方QQ群:112851260

官方邮箱:security#ihonker.org(#改成@)

官方核心成员

关注微信公众号

Archiver|手机版|小黑屋| ( 沪ICP备2021026908号 )

GMT+8, 2026-9-18 13:33 , Processed in 0.024863 second(s), 18 queries , Gzip On, Redis On.

Powered by ihonker.com

Copyright © 2015-现在.

  • 返回顶部