createSelectorQuery 页面初始化拿不到节点信息如何解决?
发布于 4 年前 作者 xiulan82 9897 次浏览 来自 问答

每次小程序重新打开 都会报错

An SelectorQuery call is ignored because no proper page or component is found. Please considering using `SelectorQuery.in` to specify a proper one.

提示我页面或者组件找不到 ,导致wx.createSelectorQuery() 节点信息也没有,然后刷新一下就好了,这个请问大神们怎么解决啊?

方法写在onScrollHandle

onScrollHandle(e) {
if (this.sectionNavNodeRefs.length === 0) this.getSectionNodeRefs()

搜的是首页滚动内容的节点信息

getSectionNodeRefs() {
this.sections.forEach((item, index) => {
wx.createSelectorQuery().select(`#${item.name}`).boundingClientRect((res) => {
if (res) {
item.nodeRefs = res
this.sectionTops.push(res.top)
}
}).exec()

wx.createSelectorQuery().select(`#nav${item.name}`).boundingClientRect((res) => {
if (res) {
console.log(res)
this.sectionNavNodeRefs.push(res)
}
}).exec()

})

BTW :SelectorQuery.in(this)没用,用的MPVUE,

程序是除了第一次打开工具的时候才会报错,之后刷新都是可以运行的,应该是API的问题,如何解决?

5 回复

请在wxml加载结束后再执行wx.createSelectorQuery(),一般会写到onReady里。

我今天也遇到这个问题,最后做了个执行函数,直到能获取到数据

你好,这是目前开发者工具上的一个已知 bug ,只在刚启动开发者工具窗口时出现。我们之后会修复。

你好,请提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

截图看看你这个方法写到哪里的,还有,你要搜的是啥

回到顶部