|
|
- ~% T) G, }1 [6 I% n, K 创建 Pinia 全局 Store新建文件 src/stores/device.ts。我们把设备监听逻辑直接写在 Store 内部。typescriptimport { defineStore } from 'pinia'
# O% [2 i5 B1 vimport { ref } from 'vue'
- z) ?$ g% B* E4 W, ^) R/ i& m1 X, x; y! j' Z C/ d
export const useDeviceStore = defineStore('device', () => {
2 k3 g4 y. x5 ? const isMobile = ref(false)* J1 e8 i' [: x
let mediaQuery: MediaQueryList | null = null9 [+ J0 J: j- j% J) i3 |7 e
2 [! J" {+ z) J9 S) @$ P. Q const updateDevice = (e: MediaQueryListEvent | MediaQueryList) => {
; P L6 ?9 ]" t1 C isMobile.value = e.matches
. V% n! K+ S0 ]- d7 u9 m. | }
6 N- i, H! ~1 V3 o( c& l) g3 r1 P% R" Y1 u
// |
|