uni-app 版本3.1.11.20210423 uni.createSelectorQuery()报错

uni-app 版本3.1.11.20210423 uni.createSelectorQuery()报错

开发环境信息

类别 信息
产品分类 uniapp/App
PC操作系统 Windows
PC系统版本 win10 64位 专业版
HBuilderX 正式
HBuilderX版本 3.1.11
手机系统 Android
手机系统版本 Android 11
手机厂商 华为
手机机型 Mate 30
页面类型 vue
打包方式 云端
项目创建方式 HBuilderX

示例代码

mounted() {  
    console.log("mounted")  
    setTimeout(() => {  
          uni.createSelectorQuery()  
    }, 1000)  
}

操作步骤

mounted() {  
    console.log("mounted")  
    setTimeout(() => {  
          uni.createSelectorQuery()  
    }, 1000)  
}

预期结果

可正常使用

实际结果

报错 Uncaught TypeError: Cannot read property ‘$vm’ of undefined

bug描述

在组件mounted中使用uni.createSelectorQuery()报错 Uncaught TypeError: Cannot read property ‘$vm’ of undefined

示例图片


更多关于uni-app 版本3.1.11.20210423 uni.createSelectorQuery()报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

找到是怎么触发的了,App.vue中: 代码块1: <template>
<view class="container">
<my-header></my-header>
<router-view class="router-container"></router-view>
<my-footer></my-footer>
</view>
</template> 代码块2: <template>
<view class="container">
<my-header></my-header>
<view class="page-container">
<router-view class="router-container"></router-view>
</view>
<my-footer></my-footer>
</view>
</template> 使用代码块1正常,使用代码块2可以复现此问题,区别在于router-view标签是否被一层父元素包裹

更多关于uni-app 版本3.1.11.20210423 uni.createSelectorQuery()报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个在特定版本中出现的组件生命周期时序问题。在uni-app 3.1.11版本中,当在mounted钩子中使用setTimeout延迟执行uni.createSelectorQuery()时,组件实例可能已经被销毁或未正确挂载,导致无法访问$vm属性。

解决方案:

  1. 直接调用而非延迟调用:
mounted() {
    uni.createSelectorQuery()
}
  1. 使用this.$nextTick确保DOM更新完成:
mounted() {
    this.$nextTick(() => {
        uni.createSelectorQuery()
    })
}
回到顶部