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

相关推荐
澄江静如练_1 小时前
微信小程序发体验版
微信小程序·小程序
颜渊呐3 小时前
uniapp中APPwebview与网页的双向通信
前端·uni-app
白杨木影子被拉长5 小时前
多状态映射不同样式(scss语法)
vue.js·uni-app
流口水的兔子5 小时前
作为一个新手,如果让你去用【微信小程序通过BLE实现与设备通讯】,你会怎么做,
前端·物联网·微信小程序
一念杂记5 小时前
免费开源!微信小程序商城源码,快速搭建你的线上商城系统!
微信小程序·uni-app
张晓~183399481217 小时前
数字人源码部署流程分享--- PC+小程序融合方案
javascript·小程序·矩阵·aigc·文心一言·html5
The_era_achievs_hero9 小时前
微信小程序61~70
微信小程序·小程序
编程猪猪侠10 小时前
Taro+Vue3实现微信小程序富文本编辑器组件开发指南
vue.js·微信小程序·taro
aklry11 小时前
uniapp三步完成生成一维码图片
uni-app
汤姆yu20 小时前
基于微信小程序的学校招生系统
微信小程序·小程序·招生小程序