提交 | 用户 | 时间
|
fd2207
|
1 |
import store from '@/store' |
懒 |
2 |
|
|
3 |
const { body } = document |
|
4 |
const WIDTH = 992 // refer to Bootstrap's responsive design |
|
5 |
|
|
6 |
export default { |
|
7 |
watch: { |
|
8 |
$route(route) { |
|
9 |
if (this.device === 'mobile' && this.sidebar.opened) { |
|
10 |
store.dispatch('app/closeSideBar', { withoutAnimation: false }) |
|
11 |
} |
|
12 |
} |
|
13 |
}, |
|
14 |
beforeMount() { |
|
15 |
window.addEventListener('resize', this.$_resizeHandler) |
|
16 |
}, |
|
17 |
beforeDestroy() { |
|
18 |
window.removeEventListener('resize', this.$_resizeHandler) |
|
19 |
}, |
|
20 |
mounted() { |
|
21 |
const isMobile = this.$_isMobile() |
|
22 |
if (isMobile) { |
|
23 |
store.dispatch('app/toggleDevice', 'mobile') |
|
24 |
store.dispatch('app/closeSideBar', { withoutAnimation: true }) |
|
25 |
} |
|
26 |
}, |
|
27 |
methods: { |
|
28 |
// use $_ for mixins properties |
|
29 |
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential |
|
30 |
$_isMobile() { |
|
31 |
const rect = body.getBoundingClientRect() |
|
32 |
return rect.width - 1 < WIDTH |
|
33 |
}, |
|
34 |
$_resizeHandler() { |
|
35 |
if (!document.hidden) { |
|
36 |
const isMobile = this.$_isMobile() |
|
37 |
store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop') |
|
38 |
|
|
39 |
if (isMobile) { |
|
40 |
store.dispatch('app/closeSideBar', { withoutAnimation: true }) |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 |
} |