feat(webview): add dynamic navigation bar title setting function

新增webview页面根据传入参数动态设置导航栏标题的功能,支持通过enable_set_title参数控制是否强制替换默认标题,通过title参数传递自定义标题内容。
This commit is contained in:
2026-07-15 10:38:31 +08:00
parent 229c6aa969
commit 6c3f90bf96

View File

@@ -14,10 +14,15 @@ import { decodeEntity } from '@/common/js/util.js';
export default { export default {
data() { data() {
return { return {
src: '' src: '',
title: '', // 标题栏标题
enableSetTitle: false, // 是否强制设置标题,用来替换Webview中的默认标题
}; };
}, },
onLoad(option) { onLoad(option) {
this.title = option?.title ?? '';
this.enableSetTitle = option?.enable_set_title == 1;
// #ifdef H5 // #ifdef H5
this.src = decodeEntity(decodeURIComponent(option.src)); this.src = decodeEntity(decodeURIComponent(option.src));
// #endif // #endif
@@ -26,11 +31,32 @@ export default {
this.src = decodeEntity(decodeURIComponent(`${option.src}&from=mp-webview`)); this.src = decodeEntity(decodeURIComponent(`${option.src}&from=mp-webview`));
// #endif // #endif
}, },
onReady() {
this.updateNavbarTitle();
},
onShow() {
this.updateNavbarTitle();
},
watch: {
title(newVal) {
this.updateNavbarTitle(newVal);
},
enableSetTitle() {
this.updateNavbarTitle();
}
},
methods: { methods: {
navigateBack() { navigateBack() {
uni.navigateBack({ uni.navigateBack({
delta: 1 delta: 1
}); });
},
updateNavbarTitle(newTitle) {
if (this.enableSetTitle) {
uni.setNavigationBarTitle({
title: newTitle ?? this.title
});
}
} }
} }
}; };