biaov
biaov
  • 发布:2022-12-06 16:03
  • 更新:2022-12-06 16:03
  • 阅读:908

在 @vue/composition-api 中使用 createSelectorQuery 的 this 问题

分类:uni-app
  • 当你在 @vue/composition-api 使用 createSelectorQuery 时获取不到 this, 则使用 getCurrentInstance 替换
  • 演示代码:
import { getCurrentInstance } from '@vue/composition-api'  

export default {  
  setup() {  
    const app = getCurrentInstance()  
    uni  
      .createSelectorQuery()  
      .in(app.proxy)  
      .select('#myCanvas')  
      .node(data => {  
        console.log(data) // 输出:{ nodeCanvasType: "2d", node: Cy }  
      })  
      .exec()  
  }  
}
  • 如果是vue3则去掉 proxy 即可
import { getCurrentInstance } from '@vue/composition-api'  

export default {  
  setup() {  
    const app = getCurrentInstance()  
    uni  
      .createSelectorQuery()  
      .in(app)  
      .select('#myCanvas')  
      .node(data => {  
        console.log(data) // 输出:{ nodeCanvasType: "2d", node: Cy }  
      })  
      .exec()  
  }  
}
0 关注 分享

要回复文章请先登录注册