定义一个属性记录宽度
const screenWidth = ref(window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth)在 vue mounted 的时候 去挂载一下 window.onresize 方法
onMounted(() => {
window.onresize = () => {
return (() => {
screenWidth.value = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
})()
}
})去监听这个 属性值的变化,如果发生变化则讲这个 val 传递给 this.screenWidth
watch(() => screenWidth, (val) => {
screenWidth.value = val
})这样screenWidth就跟随浏览器的窗口大小动态变化了
进行使用
const programHaplomultiple = computed(() => {
return (screenWidth.value * 0.52) / infoContent.value.width / 320
})
文章介绍了如何在Vue应用中定义一个属性`screenWidth`来记录浏览器窗口的宽度。在`onMounted`生命周期钩子中,设置`window.onresize`事件监听窗口大小变化。同时,使用`watch`来同步`screenWidth`的新值。示例还展示了如何基于`screenWidth`动态计算`programHaplomultiple`的值。



837

被折叠的 条评论
为什么被折叠?



