微信小程序 WXML 文件中 styles 属性使用错误的解决方法
微信小程序 WXML 文件中 styles 属性使用错误的解决方法
在微信小程序开发中,经常会遇到在 WXML 文件中使用 styles='{{position:'fixed',width:'560rpx',height:'338rpx',top:0,bottom:0,left:0,right:0,margin:'auto',backgroundColor:'#FFF',borderRadius:'12rpx',zIndex:10}}' 这样的写法编译出错的情况。
这是因为 WXML 文件中的 styles 属性值只能是一个字符串,而不能是包含多个样式属性的对象。
为了解决这个问题,可以使用以下两种方式之一:
1. 在 WXML 文件中使用内联样式:
<view style='position: fixed; width: 560rpx; height: 338rpx; top: 0; bottom: 0; left: 0; right: 0; margin: auto; background-color: #FFF; border-radius: 12rpx; z-index: 10;'></view>
2. 在 WXSS 文件中定义类样式:
<view class='my-style'></view>
.my-style {
position: fixed;
width: 560rpx;
height: 338rpx;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background-color: #FFF;
border-radius: 12rpx;
z-index: 10;
}
请根据您的需求选择其中一种方式来设置样式。
原文地址: https://www.cveoy.top/t/topic/pyJQ 著作权归作者所有。请勿转载和采集!