feat(mixins/bubblecallable): make component able to call its parents method

This commit is contained in:
07akioni 2019-08-16 16:46:47 +08:00
parent b90a61821f
commit 9efd312f99

View File

@ -0,0 +1,25 @@
export default {
methods: {
bubbleCall (componentName, functionName, ...params) {
let parent = this.$parent || this.$root
while (true) {
if (parent) {
const name = parent.$options.name
if (Array.isArray(componentName)) {
if (componentName.includes(name)) {
parent[functionName](...params)
break
}
} else {
if (name === componentName) {
parent[functionName](...params)
break
}
}
}
if (!parent || parent === this.$root) break
parent = parent.$parent
}
}
}
}