Component组件如何获取子组件对象?

小程序 文章 2021-12-13 18:00 618 0 全屏看文

AI助手支持GPT4.0

Component组件如何获取子组件对象?How does the Component component obtain the child component object?

Component组件如何获取子组件对象?
// page.wxml<view>  <a id="test1"></a></view>// a.wxml<view>  <b id="test2"></b></view>// b.wxml<view>测试</view>// a,b均为自定义组件,且b为a的子组件// page页面,在开发者工具终端执行以下代码let pages = getCurrentPages();let currentPage = pages[pages.length - 1];let componentA = currentPage.selectComponent("#test1"); // 可以获取到组件alet componentB = currentPage.selectComponent("#test1").selectComponent("#test2"); // 获取不到组件b,返回null想问下各位大佬,怎么在自定义组件中获取到其子组件对象

// page. Wxml < View > < a id = "test1" > < / a > < / View > / / a.wxml < View > < B id = "test2" > < / b > < / View > / / b.wxml < View > test < / View > / / A and B are user-defined components, and B is a sub component of a. / / page page page. Execute the following code on the developer tool terminal, let pages = getcurrentpages(); let currentPage = pages[pages.length - 1]; let componentA = currentPage. selectComponent("#test1"); // You can get the component alert componentb = CurrentPage selectComponent("#test1"). selectComponent("#test2"); // If component B cannot be obtained, null is returned. I would like to ask you how to obtain its sub component object in the custom component

回答:

田文杰:
已解决,是id加错了位置,连续selectComponent是可以的。
xplee:

直接currentPage.selectComponent("#test2")

田文杰:刚刚写错了,test2是定义在component文件中,直接获取会报错。问题已修改~
brave:

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

-EOF-

AI助手支持GPT4.0