How to find any component instance
From a component, find the nearest specified component upwards
/** * From a component, find the nearest specified component upwards * @param {*} context The current context, for example, which component you want to search upward, is generally based on the current component, that is, this is passed in * @param {*} componentName name of the component to be found */ function findComponentUpward(context, componentName) { let parent = context.$parent let name = parent.$ while (parent && (!name || [componentName].indexOf(name) < 0)) { parent = parent.$parent if (parent) name = parent.$ } return parent }
From a component, find all specified components upwards
/** * From a component, find all specified components upwards * @param {*} context The current context, for example, which component you want to search upward, is generally based on the current component, that is, this is passed in * @param {*} componentName name of the component to be found */ function findComponentsUpward(context, componentName) { let parents = [] const parent = context.$parent if (parent) { if (parent.$ === componentName) (parent) return (findComponentsUpward(parent, componentName)) } else { return [] } }
From a component, find the nearest specified component downwards
/** * From a component, find the nearest specified component downwards * @param {*} context The current context, for example, which component you want to search upward, is generally based on the current component, that is, this is passed in * @param {*} componentName name of the component to be found */ function findComponentDownward(context, componentName) { const childrens = context.$children let children = null if () { for (const child of childrens) { const name = child.$ if (name === componentName) { children = child break } else { children = findComponentDownward(child, componentName) if (children) break } } } return children }
From a component, find all specified components downwards
/** * From a component, find all specified components downwards * @param {*} context The current context, for example, which component you want to search upward, is generally based on the current component, that is, this is passed in * @param {*} componentName name of the component to be found */ function findComponentsDownward(context, componentName) { return context.$((components, child) => { if (child.$ === componentName) (child) const foundChilds = findComponentsDownward(child, componentName) return (foundChilds) }, []) }
From a component, find the sibling components of the specified component
/** * From a component, find the sibling components of the specified component * @param {*} context The current context, for example, which component you want to search upward, is generally based on the current component, that is, this is passed in * @param {*} componentName name of the component to be found * @param {*} exceptMe Does it exclude itself */ function findBrothersComponents(context, componentName, exceptMe = true) { let res = context.$parent.$((item) => { return item.$ === componentName }) let index = ((item) => item._uid === context._uid) if (exceptMe) (index, 1) return res }
Vue Common Component Library
Common component library for mobile terminals
/vant
/cube-ui
///JD.com's own
Common component library for PC
/
The above is personal experience. I hope you can give you a reference and I hope you can support me more.