微信小程序/uniapp:class和style不生效的问题

非常重要:小程序端不支持 classObject 和 styleObject 语法。

文档:https://uniapp.dcloud.net.cn/tutorial/vue-basics.html#class-与-style-绑定

目录

对象语法

html 复制代码
<!-- class -->
<view class="static" :class="{ active: isActive}">111</view>

<!-- style -->
<view :style="{ color: activeColor, fontSize: fontSize + 'px' }">333</view>

数组语法

html 复制代码
<!-- class -->
<view class="static" :class="['activeClass', 'errorClass']">111</view>

<!-- style -->
<view :style="[{ color: activeColor, fontSize: fontSize + 'px' }]">444</view>

字符串语法

html 复制代码
<!-- class -->
<view class="static" :class="computedClassStr">111</view>

<!-- style -->
<view :style="computedClassStr">444</view>

computed

html 复制代码
 <!-- 支持 -->
<view class="container" :class="computedClassStr"></view>
<view class="container" :class="{active: isActive}"></view>

<!-- 不支持 -->
<view class="container" :class="computedClassObject"></view>

其他方案

使用第三方库classnames,来处理类名,可以将ClassObject转为ClassStr

具体用法可以参看:https://pengshiyu.blog.csdn.net/article/details/134287806

相关推荐
2501_915909063 小时前
IPA 深度混淆是什么意思?从混淆强度到实际效果的解读
android·ios·小程序·https·uni-app·iphone·webview
莫名的好感°4 小时前
2026家庭快消品采购小程序实测体验
小程序
蜡台4 小时前
修改Uniapp input 组件不刷新视图问题
前端·uni-app·input
黄华SJ520it16 小时前
新零售排队免单系统开发全流程介绍
小程序·零售·系统开发
宸翰1 天前
uni-app 设置 Android 底部虚拟导航栏背景色
android·前端·uni-app
千逐681 天前
UniApp 鸿蒙实战:音视频与多媒体处理完全指南
华为·uni-app·harmonyos·鸿蒙
handsome09161 天前
windows开发uniapp如何上架app到app store
uni-app·上架
宠友信息1 天前
Redis 内容社区源码架构优化与即时通讯数据一致性处理
spring boot·后端·websocket·mysql·uni-app
CHB2 天前
uni-app x 蒸汽模式 iOS版 性能测试基准报告 Benchmark
ios·uni-app·swiftui
衍生星球2 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot