vue 寻任意组件实例(iview 源码)二七惨案
findComponentUpward
findComponentsUpward
国企备忘录>强捻纱由⼀个组件,向上到所有的指定组件;
findComponentDownward
由⼀个组件,向下到最近的指定组件;
findComponentsDownward
由⼀个组件,向下到所有指定的组件;
findBrothersComponents
由⼀个组件,到指定组件的兄弟组件// Find components upward function findComponentUpward (co
ntext , componentName , componentNames ) { if (typeof componentName === 'string') { componentNames = [componentName ]; } else { componentNames = componentName ; } let parent = context .$parent ; let name = parent .$options .name ; while (parent && (!name || componentNames .indexOf (name ) < 0)) { parent = parent .$parent ; if (parent ) name = parent .$options .name ; } return parent ;}export {findComponentUpward };// Find component downward export function findComponentDownward (context , componentName ) { const childrens = context .$children ; let children = null ; if (childrens .length ) { for (const child of childrens ) { const name = child .$options .name ; if (name === componentName ) { children = child ; break ; } else { children = findComponentDownward (child , componentName ); if (children ) break ; } } } return children ;}// Find components downward export function findComponentsDownward (context , componentName , ignoreComponentNames = []) { if (!Array .isArray (ignoreComponentNames )) { ignoreComponentNames = [ignoreComponentNames ]; } return context .$children .reduce ((components , child ) => { if (child .$options .name === componentName ) components .push (child ); if (ignoreComponentNames .indexOf (child .$options .name ) < 0) { const foundChilds = findComponentsDownward (child , componentName ); return components .concat (foundChilds ); } else { return components ;
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
nc440033
34
35
36
37
38
39
40
41
42
43
44
45
46
已来的主人翁47
48
49
50
后结构主义
return components ; } }, []);}// Find components upward export function findComponentsUpward (context , componentName ) { let parents = []; const parent = context .$parent ; if (parent ) { if (parent .$options .name === componentName ) parents .push (parent ); return parents .concat (findComponentsUpward (parent , componentName )); } else { return []; }}// Find brothers components export function findBrothersComponents (context , componentName , exceptMe = true ) { let res = context .$parent .$children .filter (item => { return item .$options .name === componentName ; }); let index = res .findIndex (item => item ._uid === context ._uid ); if (exceptMe ) res .splice (index , 1); return res ;}5051525354
555657585960616263646566676869707172737475