提交 b4139244 编写于 作者: inkwalk's avatar inkwalk 提交者: qiang

fixed: match-media can`t work in HX3.0

上级 e44961b5
......@@ -2,7 +2,9 @@ import createCallbacks from 'uni-helpers/callbacks'
import {
getCurrentPageVm
} from '../../platform'
import {
checkInWindows
} from 'uni-helpers/windows'
const createMediaQueryObserverCallbacks = createCallbacks('requestMediaQueryObserver')
class ServiceMediaQueryObserver {
......@@ -24,13 +26,13 @@ class ServiceMediaQueryObserver {
reqId: this.reqId,
component: this.component,
options: this.options
})
}, checkInWindows(this.component) ? this.component : this.pageId)
}
disconnect () {
UniServiceJSBridge.publishHandler('destroyMediaQueryObserver', {
reqId: this.reqId
})
}, checkInWindows(this.component) ? this.component : this.pageId)
}
}
......
......@@ -34,6 +34,18 @@ export function requestMediaQueryObserver ({
reqId,
options
}, pageId) {
let pageVm
if (pageId._isVue) {
pageVm = pageId
} else {
const pages = getCurrentPages() // 跨平台时,View 层也应该实现该方法,举例 App 上,View 层的 getCurrentPages 返回长度为1的当前页面数组
const page = pages.find(page => page.$page.id === pageId)
if (!page) {
throw new Error(`Not Found:Page[${pageId}]`)
}
pageVm = page.$vm
}
// 创建一个媒体查询对象
const mediaQueryObserver = mediaQueryObservers[reqId] = window.matchMedia(handleMediaQueryStr(options))
......@@ -42,7 +54,7 @@ export function requestMediaQueryObserver ({
UniViewJSBridge.publishHandler('onRequestMediaQueryObserver', {
reqId,
res: e.matches
})
}, pageVm.$page.id)
}
listener(mediaQueryObserver) // 监听前执行一次媒体查询
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册