Jon*_*ele 4 window screen width reactive-programming vue.js
是否有一个反应式窗口宽度,其中变量或数据属性跟踪窗口调整大小
例如
computed:{
smallScreen(){
if(window.innerWidth < 720){
this.$set(this.screen_size, "width", window.innerWidth)
return true
}
return false
}
Run Code Online (Sandbox Code Playgroud)
小智 12
我认为除非您在窗口上附加了侦听器,否则没有其他方法可以做到这一点。您可以windowWidth在组件的属性上添加一个属性,data并附加调整大小侦听器,该侦听器将在安装组件时修改该值。
尝试这样的事情:
<template>
<p>Resize me! Current width is: {{ windowWidth }}</p>
</template
<script>
export default {
data() {
return {
windowWidth: window.innerWidth
}
},
mounted() {
window.onresize = () => {
this.windowWidth = window.innerWidth
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
小智 5
如果您在此解决方案中使用多个组件,则接受的答案的调整大小处理程序函数将仅更新最后一个组件。
那么你应该改用这个:
import { Component, Vue } from 'vue-property-decorator';
@Component
export class WidthWatcher extends Vue {
public windowWidth: number = window.innerWidth;
public mounted() {
window.addEventListener('resize', this.handleResize);
}
public handleResize() {
this.windowWidth = window.innerWidth;
}
public beforeDestroy() {
window.removeEventListener('resize', this.handleResize);
}
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/vuejs/vue/issues/1915#issuecomment-159334432
| 归档时间: |
|
| 查看次数: |
4057 次 |
| 最近记录: |