Files
lucky_shop/pages_tool/webview/webview.vue
ZF sun b10bf472e3 feat(webview): add webview title auto update and debug logs
1. add @titleUpdate event listener for web-view component
2. decode uri component for title and enableSetTitle params
3. refactor updateNavbarTitle to accept dynamic new title
4. add debug console logs for tracking title update flow
5. add success/fail callbacks for navigation bar title setting
2026-07-15 11:55:52 +08:00

83 lines
2.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view :style="themeColor">
<view class="iconfont iconshang navigate-back" @click="navigateBack"></view>
<web-view :src="src" @titleUpdate="updateNavbarTitle"></web-view>
<!-- 隐私弹窗 -->
<privacy-popup ref="privacyPopup"></privacy-popup>
</view>
</template>
<script>
import { decodeEntity } from '@/common/js/util.js';
export default {
data() {
return {
src: '',
title: '', // 标题栏标题
enableSetTitle: false, // 是否强制设置标题,用来替换Webview中的默认标题
};
},
onLoad(option) {
console.log(`webview .......................................`)
this.title = decodeURIComponent(option?.title ?? '');
this.enableSetTitle = decodeURIComponent(option?.enable_set_title) == 1;
console.log(`webview -------------------`, { title: this.title, enableSetTitle: this.enableSetTitle });
// #ifdef H5
this.src = decodeEntity(decodeURIComponent(option.src));
// #endif
// #ifndef H5
this.src = decodeEntity(decodeURIComponent(`${option.src}&from=mp-webview`));
// #endif
},
onShow() {
console.log('webview onShow ------------------------');
this.updateNavbarTitle();
},
watch: {
title(newVal) {
this.updateNavbarTitle(newVal);
},
enableSetTitle() {
this.updateNavbarTitle();
}
},
methods: {
navigateBack() {
uni.navigateBack({
delta: 1
});
},
updateNavbarTitle(newTitle) {
const title = newTitle ?? this.title;
console.log('webview updateNavbarTitle', title);
if (this.enableSetTitle) {
uni.setNavigationBarTitle({
title: title,
success: function (res) {
console.log('webview updateNavbarTitle success !!')
},
fail: function (err) {
console.error('webview updateNavbarTitle fail !!', err);
}
});
}
}
}
};
</script>
<style lang="scss">
.navigate-back {
position: absolute;
top: 34rpx;
left: 34rpx;
z-index: 5;
font-size: $font-size-toolbar;
}
</style>