uni-app 样式计算在H5和微信中是不同的

uni-app 样式计算在H5和微信中是不同的

开发环境 版本号 项目创建方式
Windows HBuilderX

产品分类:uniapp/小程序/微信

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:windows10

HBuilderX类型:正式

HBuilderX版本号:3.2.16

第三方开发者工具版本号:3.2.16.20211122

基础库版本号:什么基础库?

项目创建方式:HBuilderX


示例代码:

<template>
<view :style="imagebox">
<lqz-banner :AdvName="item.advName" :height="item.height+'px'"  [@click](/user/click)="clickView(item.options)"></lqz-banner>
</view>
</template>
computed: {
imagebox: function() {
let boxclass = {
height: this.item.height + 'px',
'margin-left': this.item.margin_left + 'px',
'margin-right': this.item.margin_right + 'px',
'margin-top': this.item.margin_top + 'px',
'margin-bottom': this.item.margin_bottom + 'px',
'padding-bottom': this.item.padding_bottom + 'px',
'padding-left': this.item.padding_left + 'px',
'padding-right': this.item.padding_right + 'px',
'padding-top': this.item.padding_top + 'px'
}
// #ifdef MP-WEIXIN
return JSON.stringify(boxclass);
// #endif
// #ifndef MP-WEIXIN
return boxclass;
// #endif
}
},

操作步骤:

<template>
<view :style="imagebox">
<lqz-banner :AdvName="item.advName" :height="item.height+'px'"  [@click](/user/click)="clickView(item.options)"></lqz-banner>
</view>
</template>
computed: {
imagebox: function() {
let boxclass = {
height: this.item.height + 'px',
'margin-left': this.item.margin_left + 'px',
'margin-right': this.item.margin_right + 'px',
'margin-top': this.item.margin_top + 'px',
'margin-bottom': this.item.margin_bottom + 'px',
'padding-bottom': this.item.padding_bottom + 'px',
'padding-left': this.item.padding_left + 'px',
'padding-right': this.item.padding_right + 'px',
'padding-top': this.item.padding_top + 'px'
}
// #ifdef MP-WEIXIN
return JSON.stringify(boxclass);
// #endif
// #ifndef MP-WEIXIN
return boxclass;
// #endif
}
},

更多关于uni-app 样式计算在H5和微信中是不同的的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

应该不是uniapp的问题,是微信小程序不支持

更多关于uni-app 样式计算在H5和微信中是不同的的实战教程也可以访问 https://www.itying.com/category-93-b0.html


小程序端不支持 classObject 和 styleObject 语法。文档

这是 uni-app 中常见的跨平台样式处理问题。在 H5 平台,Vue 可以直接处理对象形式的样式绑定,但在微信小程序中,样式绑定需要字符串形式。

你的代码处理方式是正确的:

  • 在 H5 等平台直接返回样式对象
  • 在微信小程序中通过 JSON.stringify() 将对象转为字符串

这种差异的原因是:

  1. H5 平台:基于 Vue 的运行时,支持对象形式的样式绑定
  2. 微信小程序:需要将样式编译为小程序模板语法,只支持字符串形式的样式

建议优化:

imagebox: function() {
    const style = {
        height: `${this.item.height}px`,
        marginLeft: `${this.item.margin_left}px`,
        marginRight: `${this.item.margin_right}px`,
        marginTop: `${this.item.margin_top}px`,
        marginBottom: `${this.item.margin_bottom}px`,
        paddingBottom: `${this.item.padding_bottom}px`,
        paddingLeft: `${this.item.padding_left}px`,
        paddingRight: `${this.item.padding_right}px`,
        paddingTop: `${this.item.padding_top}px`
    };
    
    // #ifdef MP-WEIXIN
    // 微信小程序需要字符串形式
    return Object.entries(style)
        .map(([key, value]) => `${key}:${value}`)
        .join(';');
    // #endif
    
    // #ifndef MP-WEIXIN
    // 其他平台直接返回对象
    return style;
    // #endif
}
回到顶部