修复小程序内嵌页面标题同步
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-07-27 19:56:53 +08:00
parent 9ea1bfab71
commit f1047414bb
6 changed files with 113 additions and 3 deletions

View File

@@ -0,0 +1,39 @@
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'
export const MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE = 'museum-guide:navigation-title'
type MiniProgramBridge = {
postMessage?: (options: {
data: {
type: typeof MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE
title: string
}
}) => void
}
const getMiniProgramBridge = () => {
if (typeof window === 'undefined') return undefined
return (window as Window & {
wx?: {
miniProgram?: MiniProgramBridge
}
}).wx?.miniProgram
}
export const syncHostNavigationTitle = (title: string) => {
if (typeof document !== 'undefined') document.title = title
if (typeof uni !== 'undefined' && typeof uni.setNavigationBarTitle === 'function') {
uni.setNavigationBarTitle({ title })
}
if (!isEmbeddedInWechatMiniProgram()) return
getMiniProgramBridge()?.postMessage?.({
data: {
type: MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE,
title
}
})
}