york-demo-ui
Version:
65 lines (64 loc) • 1.88 kB
JavaScript
function findComponentUpward (context, companyName) {
//获取父组件
let parent = context.$parent
//获取父组件的name属性
let name = parent.$options.name
// 找不到继续向上查找
while (parent && (!name || [companyName].indexOf(name) < 0)){
parent = parent.$parent
if(parent) name = parent.$options.name
}
//返回
return parent
}
function findComponentsUpward (context, companyName) {
// 收集父组件
let parents = []
// 获取父组件
const parent = context.$parent
if (parent) {
if (parent.$options.name === companyName) parents.push(parent)
return parents.concat(findComponentUpward(parent, companyName))
} else {
return []
}
}
function findComponentDownward (context, componentName) {
let children = null
let childrens = context.$children
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
}
function findComponentsDownward (context, componentName) {
return context.$children.reduce((components, child) => {
if (child.$options.name === componentName) components.push(child)
const foundChilds = findComponentsDownward(child, componentName)
return components.concat(foundChilds)
}, [])
}
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
}
export {
findComponentUpward,
findComponentsUpward,
findComponentDownward,
findComponentsDownward,
findBrothersComponents
}