Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): 新增H5端app.onShow和app.onHide生命周期 #15183

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/taro-router/src/router/mpa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ export async function createMultiRouter (
handler.load(page, pageConfig)

app.onShow?.(launchParam as Record<string, any>)

window.addEventListener('visibilitychange', ()=>{
if (document.visibilityState === 'visible') {
app.onShow?.(launchParam as Record<string, any>)
}else{
app.onHide?.(launchParam as Record<string, any>)
}
})
}
8 changes: 8 additions & 0 deletions packages/taro-router/src/router/spa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,13 @@

app.onShow?.(launchParam as Record<string, any>)

window.addEventListener('visibilitychange', ()=>{
if (document.visibilityState === 'visible') {

Check warning on line 214 in packages/taro-router/src/router/spa.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/router/spa.ts#L214

Added line #L214 was not covered by tests
app.onShow?.(launchParam as Record<string, any>)
}else{
app.onHide?.(launchParam as Record<string, any>)
}
})

return history.listen(render)
}
2 changes: 1 addition & 1 deletion packages/taro-router/src/tabbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function initTabbar (config: AppConfig, history: History) {

// TODO: custom-tab-bar
defineCustomElementTaroTabbar()
const tabbar: any = document.createElement('taro-tabbar') as HTMLDivElement
const tabbar: any = document.createElement('taro-tabbar') as HTMLElement
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '')
tabbar.conf = config.tabBar
tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname
Expand Down
1 change: 1 addition & 0 deletions packages/taro-runtime/src/dsl/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface AppInstance extends Show {
onPageNotFound? (res: any): void
onUnhandledRejection? (error: any): void
onShow?(options?: Record<string, unknown>): void
onHide?(options?: Record<string, unknown>): void
unmount? (id: string, cb?: () => void): void
taroGlobalData?: Record<any, any>
config?: Record<any, any>
Expand Down
Loading