I am building a vue web app which will be used across all devices. I have some code which I want to get executed only on small devices or mobile. Currently I have that code in a if condition with $(window).width()
, like following:
if ($(window).width() <= 768) {
//My mobile specific code
}
Is there some better way or vue way of doing this?
Edit
For example in one of the component I have:
export default {
data () {
return {
fade: false,
showFilter: $(window).width() > 768
}
},
components: { PrFilter, Pr },
created () {
if ($(window).width() <= 768) {
bus.$on('pr-changed', () => {
this.showFilter = false
this.fade = false
})
}
}
}
in another component, I have:
watch: {
matchingPr: function (filteredPr) {
if (filteredPr.length) {
this.pr = filteredPr[0]
if ($(window).width() <= 768) {
bus.$emit('pr-changed')
}
}
}
},