Skip to content

Instantly share code, notes, and snippets.

@benounnas
Created April 17, 2020 14:11
Show Gist options
  • Save benounnas/b084dcb5c97ebc15fe93c6c7668d850b to your computer and use it in GitHub Desktop.
Save benounnas/b084dcb5c97ebc15fe93c6c7668d850b to your computer and use it in GitHub Desktop.
breakpoints programatically in vue
<script>
export default {
data: () => ({
// Create a reactive data property to track
// whether the user is on a mobile device
isMobile: false,
}),
beforeDestroy () {
if (typeof window !== 'undefined') {
// Remove the resize event when the component is destroyed
window.removeEventListener('resize', this.onResize, { passive: true })
}
},
mounted () {
// Check whether the user's device is mobile or not
this.onResize()
// Create resize event to check for mobile device
window.addEventListener('resize', this.onResize, { passive: true })
},
methods: {
onResize () {
// Set reactive data property to true
// if device is less than 600 pixels
this.isMobile = window.innerWidth < 600
},
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment