quanyawei
2023-10-31 5fecc9e46d448df3de987504440c4fdd582f858e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * 获取当前页面上下文
 * @returns 页面对象
 */
export function getContext() {
    // eslint-disable-next-line no-undef
    const pages = getCurrentPages()
    return pages[pages.length - 1]
}
 
/**
 * 获取上下文中指定节点组件
 * @param context 选择器的选择范围,可以传入自定义组件的 this 作为上下文
 * @param selector 自定义节点选择器
 */
export function getComponent(context, selector ) {
    let component = null
    // #ifdef H5
    context.$children.forEach((child) => {
        if (`#${child.$attrs.id}` === selector) {
            component = child
        } else if (child.$children && child.$children.length) {
            if (getComponent(child, selector)) {
                component = getComponent(child, selector)
            }
        }
        if (component) {
            return component
        }
    })
    // #endif
    // #ifdef MP-WEIXIN
    component = context.selectComponent && context.selectComponent(selector) && context.selectComponent(selector).$vm
    // #endif
 
    // #ifdef MP-ALIPAY
    const alipay = context.$children ? context.$children : context.$vm && context.$vm.$children ? context.$vm
        .$children : []
    component = alipay.find((component) => {
        return `#${component.$scope.props.id}` === selector
    })
    // #endif
    // #ifdef APP-PLUS
    const app = context.$children ? context.$children : context.$vm && context.$vm.$children ? context.$vm.$children :
    []
    component = app.find((component) => {
        return `#${component.$attrs.id}` === selector
    })
    // #endif
    return component
}