Created
April 17, 2020 14:11
-
-
Save benounnas/b084dcb5c97ebc15fe93c6c7668d850b to your computer and use it in GitHub Desktop.
breakpoints programatically in vue
This file contains hidden or 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: () => ({ | |
// 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