uni-app 微信小程序中 data初始数据为空字符串 后续设置为0无法回显到页面
uni-app 微信小程序中 data初始数据为空字符串 后续设置为0无法回显到页面
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 10 | HBuilderX |
示例代码:
<template>
<view>
{{info}}
</view>
</template>
<script>
export default {
data() {
return {
info:''
};
},
onLoad() {
this.info= 0
}
};
</script>
<style lang="scss">
</style>
操作步骤:
<template>
<view>
{{info}}
</view>
</template>
<script>
export default {
data() {
return {
info:''
};
},
onLoad() {
this.info= 0
}
};
</script>
<style lang="scss">
</style>
预期结果:
界面中的info为0
实际结果:
界面中的info为空
bug描述:
微信小程序,data中初始数据为空字符串,后续设置为0,无法回显到页面。如果赋值为字符串0或其余非0数字,则可回显。如果data中初始数据为null,则后续设置为0也可以回显。
更多关于uni-app 微信小程序中 data初始数据为空字符串 后续设置为0无法回显到页面的实战教程也可以访问 https://www.itying.com/category-93-b0.html
5 回复
使用最新版HX测试,没有此问题,可以升级后重试
更多关于uni-app 微信小程序中 data初始数据为空字符串 后续设置为0无法回显到页面的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我更新到了最新3.1.18。还是有这个问题哦。如果原始值为空字符串或字符串中仅有空格,那么再将其赋值为数字0,页面上就不会显示出0。但是在js中打印是可以打印出0的。
回复 1***@qq.com: 已确认此问题,后续会讨论修改
这是微信小程序底层数据监听机制的问题。当data初始值为空字符串时,直接赋值为数字0,小程序的数据监听系统可能无法正确触发视图更新。
解决方案:
- 使用$set强制更新
onLoad() {
this.$set(this, 'info', 0)
}
- 初始值设为null或undefined
data() {
return {
info: null
};
}
- 使用字符串’0’
onLoad() {
this.info = '0'
}
- 使用setTimeout延迟赋值(临时方案)
onLoad() {
setTimeout(() => {
this.info = 0
}, 0)
}


