您的位置:首页 > 教育 > 培训 > 微信小程序/uniapp:class和style不生效的问题

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

2024/10/5 14:05:56 来源:https://blog.csdn.net/mouday/article/details/140183136  浏览:    关键词:微信小程序/uniapp:class和style不生效的问题

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

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

目录

    • 对象语法
    • 数组语法
    • 字符串语法
    • computed
    • 其他方案

对象语法

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

数组语法

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

字符串语法

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

computed

 <!-- 支持 -->
<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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com