微信小程序 wx.createSelectorQuery 和 this.createSelectorQuery 的差异

概述

通过 SelectorQuery 可以获取页面上节点的一些信息(节点的属性、样式、位置等)
在页面和自定义组件中,wx.createSelectorQuery 和 this.createSelectorQuery 都可以创建一个 SelectorQuery 对象,本文主要讲述两种方式的差异

SelectorQuery 的相关文档

差异

在页面中使用时的差异

首先介绍一下代码细节

  • 有两个页面 index 和 another
  • 两个页面中,都有一个 id 为 page-view 的组件
  • 两个组件 data-source 属性的值不同
  • 点击 index 中的按钮时,延时打印 page-view 节点的一些信息(id 和自定义属性),并启动 another

index WXML

<button bind:tap="onTap">打开 another</button>
<view id="page-view" data-source="index">index → view</view>

index JS

Page({
  showFields(root, selector, tag) {
    root.createSelectorQuery().select(selector)
      .fields(
        { id: true, dataset: true },
        res => console.log(tag, JSON.stringify(res))
      )
      .exec()
  },

  onTap() {
    this.showFields(wx, '#page-view', 'sync wx')
    this.showFields(this, '#page-view', 'sync this')

    setTimeout(() => {
      this.showFields(wx, '#page-view', 'async wx')
      this.showFields(this, '#page-view', 'async this')
    }, 1000)

    wx.navigateTo({ url: '/pages/api/node/another/another' })
  }
})

another WXML

<view id="page-view" data-source="another">another → view</view>

点击按钮后在控制台打印的日志

sync wx {"id":"page-view","dataset":{"source":"index"}}
sync this {"id":"page-view","dataset":{"source":"index"}}
async wx {"id":"page-view","dataset":{"source":"another"}}
async this {"id":"page-view","dataset":{"source":"index"}}

可以发现,在 index 中,通过 wx.createSelectorQuery 这种方式,获取到了 another 中的节点

在自定义组件中使用时的差异

首先介绍一下代码细节

  • 在 index 页面中,有一个自定义组件 my-component
  • index 和 my-component 中都有一个 id 为 my-component-view 的组件
  • 两个组件 data-source 属性的值不同
  • 点击 my-component 中的按钮时,打印 my-component-view 节点的一些信息(id 和自定义属性)

index WXML

<my-component></my-component>
<view id="my-component-view" data-source="index">index → view</view>

my-component WXML

<button bind:tap="onTap">showMyCmptView</button>
<view id="my-component-view" data-source="my-component">my-component → view</view>

my-component JS

Component({
  methods: {
    showFields(root, selector, tag) {
      root.createSelectorQuery().select(selector)
        .fields(
          { id: true, dataset: true },
          res => console.log(tag, JSON.stringify(res))
        )
        .exec()
    },

    onTap() {
      this.showFields(wx, '#my-component-view', 'wx')
      this.showFields(this, '#my-component-view', 'this')
    }
  }
})

点击按钮后在控制台打印的日志

wx {"id":"my-component-view","dataset":{"source":"index"}}
this {"id":"my-component-view","dataset":{"source":"my-component"}}

可以发现,在 my-component 中,通过 wx.createSelectorQuery 这种方式,获取到了 index 中的节点

说明

在页面或自定义组件中使用 this.createSelectorQuery() 时,相当于使用 wx.createSelectorQuery().in(this), 把范围限定在了页面或自定义组件内。下面是 SelectorQuery.in() 的相关文档
SelectorQuery.in(Component component)

代码仓库

代码仓库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值