admin管理员组文章数量:1377664
Currently I am storing the window.innerWidth
value to a vuex getter called screenWidth
and using it in all of my ponents. But the problem is every time I want to use it I have to 1) import { mapGetters } from 'vuex'
2) call ...mapGetters()
inside the puted property. To get rid of this problem I thought prototype injection might be a good idea. So I did this:
Vue.prototype.$screenWidth = window.innerWidth;
window.addEventListener('resize', () => {
Vue.prototype.$screenWidth = window.innerWidth;
});
But that doesn't work. How can I more easily access the screen width in my ponent without going through all the import/map stuff?
Currently I am storing the window.innerWidth
value to a vuex getter called screenWidth
and using it in all of my ponents. But the problem is every time I want to use it I have to 1) import { mapGetters } from 'vuex'
2) call ...mapGetters()
inside the puted property. To get rid of this problem I thought prototype injection might be a good idea. So I did this:
Vue.prototype.$screenWidth = window.innerWidth;
window.addEventListener('resize', () => {
Vue.prototype.$screenWidth = window.innerWidth;
});
But that doesn't work. How can I more easily access the screen width in my ponent without going through all the import/map stuff?
Share Improve this question asked Jun 14, 2019 at 9:58 TanmayTanmay 3,18910 gold badges56 silver badges90 bronze badges 01 Answer
Reset to default 7The way you're already doing it with Vuex sounds fine to me.
If you're using this in a lot of ponents then perhaps an alternative might be to use an observable object on the prototype, as in the example below. By using an object we can retain the reactivity.
Vue.prototype.$screen = Vue.observable({
width: window.innerWidth,
height: window.innerHeight
});
window.addEventListener('resize', () => {
Vue.prototype.$screen.width = window.innerWidth;
Vue.prototype.$screen.height = window.innerHeight;
});
new Vue({
el: '#app'
});
<script src="https://unpkg./[email protected]/dist/vue.js"></script>
<div id="app">
<p>Width: {{ $screen.width }}</p>
<p>Height: {{ $screen.height }}</p>
</div>
This relies on Vue.observable
, which needs Vue 2.6.0. In earlier versions of Vue you could do something similar by creating a temporary Vue instance and assigning the object to the data of that instance:
Vue.prototype.$screen = new Vue({
data: {
screen: {
width: window.innerWidth,
height: window.innerHeight
}
}
}).screen;
window.addEventListener('resize', () => {
Vue.prototype.$screen.width = window.innerWidth;
Vue.prototype.$screen.height = window.innerHeight;
});
new Vue({
el: '#app'
});
<script src="https://unpkg./[email protected]/dist/vue.js"></script>
<div id="app">
<p>Width: {{ $screen.width }}</p>
<p>Height: {{ $screen.height }}</p>
</div>
It looks horrific but that's why Vue.observable
was introduced.
Note that SO wraps these snippets in an iframe so you may not see the numbers update when you resize the browser window. For me I either had to make the window quite narrow or click the Expand snippet link to see it working.
本文标签: javascriptAccessing screen width in vue componentsStack Overflow
版权声明:本文标题:javascript - Accessing screen width in vue components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744382521a2603575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论