Doc:http://www.html5plus.org/doc/zh_cn/webview.html
步骤
- 初始化,获取webview对象
- 劫持物理返回事件(onBackPress)
- 使用back()控制webview返回
Code
data() {
return {
webView: '',
// webUrl: 'http://192.168.1.66:5500/test.html',
}
},
onLoad() {
let that = this;
// #ifdef APP-PLUS
setTimeout(() => {
that.webView = that.$mp.page.$getAppWebview().children()[0];
//如果是页面初始化调用时,需要 setTimeout 延时一下
}, 600)
// #endif
},
onBackPress(e) {
console.log("onBackPress e:" + JSON.stringify(e))
// 查找应用首页窗口对象
var h = plus.webview.getWebviewById(this.webView.id);
console.log("应用首页Webview窗口:" + h.getURL());
// 属于当前 webview 首页,则退出
// H5+ 也有canback() 来判断是否能够返回,不能返回则退出
if (h.getURL() != 'http://192.168.1.66:5500/') {
this.webView.back();
return true; // 返回true 表示不执行返回键默认操作
}
},