父组件
export default {
name: "children",
props: {},
data() {
return {}
}
}
子组件
/**
* 获取父元素实例
*/
getParent(name) {
let parent = this.$parent;
let parentName = parent.$options.name;
while (parentName !== name) {
parent = parent.$parent;
if (!parent) return false;
parentName = parent.$options.name;
}
return parent;
}
const children = this.getParent('children')
console.log('获取到父组件实例', children)