Created
May 17, 2017 09:24
-
-
Save zmmbreeze/177e59c7f9c3fab7d41fd93b71d11fdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
methods: { | |
/** | |
* find ancestor with specified component name. | |
* @param {string} name component name. | |
* @return {Vue} ancestor | |
*/ | |
findAncestor(name) { | |
var parent = this.$parent; | |
if (!parent) { | |
return; | |
} | |
const isMatch = (component) => { | |
const componentName = component.$options.name; | |
if (!componentName) { | |
return false; | |
} | |
return typeof name === 'string' | |
? (componentName === name) | |
: name(component); | |
}; | |
while (parent && !isMatch(parent)) { | |
parent = parent.$parent; | |
} | |
return parent; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment