vuejs2 - 您如何响应地切换不同显示尺寸的路线?
问题描述
是否有一种简单干净的方法来为不同的显示器提供不同的路线,当尺寸改变时更新路线?
解决方案
您只需要创建一个resize
侦听器并指定所需的断点,然后根据当前断点(窗口宽度)推送所需的路由:
代码笔:
https://codepen.io/AlekseiHoffman/pen/NWPWZQx?editors=1010
mounted() {
this.$nextTick(()=>{
window.addEventListener('resize', this.windowResizeHandler)
})
},
created() {
this.windowResizeHandler(); // check size on load as well
},
beforeDestroy() {
window.removeEventListener('resize', this.windowResizeHandler)
},
methods: {
windowResizeHandler(event) {
if (window.innerWidth > 650) {
// change route here
// this.$router.push('ROUTE_NAME').catch(err => {})
}
else {
// change route here
// this.$router.push('ROUTE_NAME').catch(err => {})
}
}
}
推荐阅读
- python - 重置 pyqt5 小部件的 glContext
- vue.js - VueJS:使用 CDN URL 而不是 NPM 导入库
- javascript - 电子产生异常
- python - python串行读取的CPU负载
- c# - 如何在 Vue 实例中使用 @ViewBag?
- android - 哪个框架最适合混合 android 应用程序
- android-studio - initState() 不显示在自动建议中
- javascript - 如何从 Jade / Pug 中的脚本中读取参数?
- python-3.x - 我想将我的 python 输出从代码内部定向到特定的文本文件
- javascript - 如何更改 HTML5 视频控制栏的背景颜色