提交 b0e7ac94 编写于 作者: 雪洛's avatar 雪洛

fix: webview组件返回问题

webview在canBack时应优先返回webview内的页面
上级 31f0cbdc
......@@ -16,3 +16,5 @@ export const WEBVIEW_UI_EVENT = 'webviewUIEvent'
export const VD_SYNC_CALLBACK = 'vdSyncCallback'
export const INVOKE_API = 'invokeApi'
export const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE'
export const WEBVIEW_INSERTED = 'webviewInserted'
export const WEBVIEW_REMOVED = 'webviewRemoved'
......@@ -22,9 +22,6 @@ function quit () {
}
function backWebview (webview, callback) {
if (!webview.__uniapp_webview) {
return callback()
}
const children = webview.children()
if (!children || !children.length) { // 有子 webview
return callback()
......@@ -53,14 +50,14 @@ function back (delta, animationType, animationDuration) {
})
}
backWebview(currentPage, () => {
const backPage = function () {
if (animationType) {
currentPage.$getAppWebview().close(animationType, animationDuration || ANI_DURATION)
webview.close(animationType, animationDuration || ANI_DURATION)
} else {
if (currentPage.$page.openType === 'redirect') { // 如果是 redirectTo 跳转的,需要制定 back 动画
currentPage.$getAppWebview().close(ANI_CLOSE, ANI_DURATION)
webview.close(ANI_CLOSE, ANI_DURATION)
}
currentPage.$getAppWebview().close('auto')
webview.close('auto')
}
pages.slice(len - delta, len).forEach(page => page.$remove())
......@@ -70,7 +67,13 @@ function back (delta, animationType, animationDuration) {
UniServiceJSBridge.emit('onAppRoute', {
type: 'navigateBack'
})
})
}
if (!currentPage.__uniapp_webview) {
return backPage()
}
const webview = currentPage.$getAppWebview()
backWebview(webview, backPage)
}
export function navigateBack ({
......
......@@ -5,7 +5,9 @@ import {
VD_SYNC_CALLBACK,
INVOKE_API,
WEBVIEW_READY,
WEB_INVOKE_APPSERVICE
WEB_INVOKE_APPSERVICE,
WEBVIEW_INSERTED,
WEBVIEW_REMOVED
} from '../../../constants'
import {
......@@ -20,6 +22,11 @@ import onVdSyncCallback from './on-vd-sync-callback'
import onInvokeApi from './on-invoke-api'
import onWebInvokeAppService from './on-web-invoke-app-service'
import onWxsInvokeCallMethod from './on-wxs-invoke-call-method'
import {
onWebviewInserted,
onWebviewRemoved
} from './on-webview-lifecycle'
export function initSubscribeHandlers () {
const {
......@@ -59,5 +66,8 @@ export function initSubscribeHandlers () {
subscribe(VD_SYNC, onVdSync)
subscribe(VD_SYNC_CALLBACK, onVdSyncCallback)
subscribe(INVOKE_API, onInvokeApi)
subscribe(INVOKE_API, onInvokeApi)
subscribe(WEBVIEW_INSERTED, onWebviewInserted)
subscribe(WEBVIEW_REMOVED, onWebviewRemoved)
}
function findPage (pageId) {
pageId = parseInt(pageId)
const page = getCurrentPages(true).find(page => page.$page.id === pageId)
if (!page) {
return console.error(`Page[${pageId}] not found`)
}
return page
}
export function onWebviewInserted (data, pageId) {
const page = findPage(pageId)
page && (page.__uniapp_webview = true)
}
export function onWebviewRemoved (data, pageId) {
const page = findPage(pageId)
page && (delete page.__uniapp_webview)
}
<template>
<uni-web-view />
</template>
<script>
<script>
import {
WEBVIEW_INSERTED,
WEBVIEW_REMOVED
} from '../../../constants'
let webview = false
const insertHTMLWebView = ({
htmlId
......@@ -87,9 +91,11 @@ export default {
src: this.$getRealPath(this.src),
webviewStyles: this.webviewStyles
})
UniViewJSBridge.publishHandler(WEBVIEW_INSERTED, {}, this.$page.id)
},
beforeDestroy () {
removeHTMLWebView()
UniViewJSBridge.publishHandler(WEBVIEW_REMOVED, {}, this.$page.id)
}
}
</script>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册