Skip to content

Instantly share code, notes, and snippets.

@adi518
Last active July 15, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adi518/daf20b62bd7f79fa07e36d1ce01adb11 to your computer and use it in GitHub Desktop.
Save adi518/daf20b62bd7f79fa07e36d1ce01adb11 to your computer and use it in GitHub Desktop.
// Compatible with Vue.js
function getElementByRef(ref, refs) {
refs = refs || this.$refs
const noRefs = !refs
if (noRefs) {
console.error(`[getElementByRef warn]: No refs found.`)
return
}
let element = refs[ref]
// Is from element?
if (element instanceof Element) {
return element
}
// Is from component?
if (refs[ref]) {
element = refs[ref].el
if (element instanceof Element) {
return element
}
}
// Ref doesn't exist
console.error(`[getElementByRef warn]: No such ref as "${ref}".`)
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment