微信小程序/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

相关推荐
CHB13 小时前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark【Android版】
android·ios·uni-app
show4331 天前
多格式导出怎么做?2026免费小程序TXT/SRT实践
开发语言·小程序·c#
AI砖家1 天前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程
show4331 天前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy2 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349302 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it2 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发
weikecms2 天前
星巴克API接口,星巴克点餐接口开发
大数据·小程序
游戏开发爱好者83 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
2501_915909063 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone