Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  106] [ 1]  / answers: 1 / hits: 16474  / 6 Years ago, wed, may 23, 2018, 12:00:00

Basically, what I need is a computed property that returns true when the window.innerwidth is equal or lower than 768px and false when it's higher than 768px.



What I did:



computed: {
isMobile() {
if (window.innerWidth <= 768) {
return true
}
return false
}
}


But that computes that property only once, and if I resize the window later, it doesn't react to that change. What can I do?


More From » vue.js

 Answers
7

Add an eventlistener to the window like so:



new Vue({
el: #app,
data() { return { windowWidth: window.innerWidth } },
mounted() {
window.addEventListener('resize', () => {
this.windowWidth = window.innerWidth
console.log(this.isMobile)
})
},
computed: {
isMobile() {
return this.windowWidth <= 768
}
}
})

[#54363] Sunday, May 20, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;