Skip to content

Instantly share code, notes, and snippets.

@matthewblewitt
Created July 16, 2020 16:34
Show Gist options
  • Save matthewblewitt/c0a66d015d872529306374d338334f17 to your computer and use it in GitHub Desktop.
Save matthewblewitt/c0a66d015d872529306374d338334f17 to your computer and use it in GitHub Desktop.
Vue - Watch for slot content changes
<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