Skip to content

Instantly share code, notes, and snippets.

@zmmbreeze
Created May 17, 2017 09:24
Show Gist options
  • Save zmmbreeze/177e59c7f9c3fab7d41fd93b71d11fdd to your computer and use it in GitHub Desktop.
Save zmmbreeze/177e59c7f9c3fab7d41fd93b71d11fdd to your computer and use it in GitHub Desktop.
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