Created
July 16, 2020 16:34
-
-
Save matthewblewitt/c0a66d015d872529306374d338334f17 to your computer and use it in GitHub Desktop.
Vue - Watch for slot content changes
This file contains 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
<script> | |
export default { | |
data() { | |
return { | |
hasSlotContent: false, | |
} | |
}, | |
methods: { | |
checkForSlotContent() { | |
let checkForContent = (hasContent, node) => { | |
return hasContent || node.tag || (node.text && node.text.trim()); | |
} | |
return this.$slots.default && this.$slots.default.reduce(checkForContent, false); | |
}, | |
}, | |
beforeUpdate() { | |
this.hasSlotContent = this.checkForSlotContent(); | |
}, | |
created() { | |
this.hasSlotContent = this.checkForSlotContent(); | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment