uni-app 百度小程序 使用 mounted 会执行两次
uni-app 百度小程序 使用 mounted 会执行两次
操作步骤:
```javascript
mounted() {
this.getData();
this.pattern.buttonColor = this.navbarBackground.background;
// this.$refs.fab.$forceUpdate();
},
```
# 预期结果:
```
request一次
```
# 实际结果:
```
request二次
```
## bug描述:
百度小程序 使用 mounted 会执行两次
更多关于uni-app 百度小程序 使用 mounted 会执行两次的实战教程也可以访问 https://www.itying.com/category-93-b0.html
5 回复
mounted里面的代码会在百度小程序执行2次,而且第一次是在onload之前执行的。
更多关于uni-app 百度小程序 使用 mounted 会执行两次的实战教程也可以访问 https://www.itying.com/category-93-b0.html
确实有这个问题,使用onReady()就没有
但自定义组件需要mounted执行,ready是页面级别的
官方确认下?
在百度小程序中,mounted 生命周期确实可能出现重复执行的情况,这通常与百度小程序平台的渲染机制有关。以下是几种常见原因及解决方案:
-
页面渲染机制差异
百度小程序的页面初始化流程可能与Vue的预期不完全一致,导致mounted被多次触发。可尝试改用onReady(小程序原生生命周期)替代:onReady() { this.getData(); this.pattern.buttonColor = this.navbarBackground.background; } -
数据响应式更新触发
若getData()中修改了响应式数据,可能引发页面重新渲染并再次触发mounted。建议通过标志位控制执行次数:data() { return { isLoaded: false } }, mounted() { if (!this.isLoaded) { this.getData(); this.pattern.buttonColor = this.navbarBackground.background; this.isLoaded = true; } } -
组件嵌套导致的重复挂载
检查页面组件是否存在非常规嵌套关系,或v-if切换导致组件重复初始化。可尝试使用onLoad替代部分逻辑:onLoad() { this.getData(); // 提前到页面加载阶段 }

