Skip to content

Commit 2c8fa42

Browse files
authored
fix: cleanup global injection resources (#1479)
1 parent df1ac5d commit 2c8fa42

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

packages/vue-i18n-core/src/i18n.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,13 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
591591
}
592592

593593
// global method and properties injection for Composition API
594+
let globalReleaseHandler: ReturnType<typeof injectGlobalFields> | null =
595+
null
594596
if (!__legacyMode && __globalInjection) {
595-
injectGlobalFields(app, i18n.global as Composer)
597+
globalReleaseHandler = injectGlobalFields(
598+
app,
599+
i18n.global as Composer
600+
)
596601
}
597602

598603
// install built-in components and directive
@@ -614,6 +619,7 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
614619
// release global scope
615620
const unmountApp = app.unmount
616621
app.unmount = () => {
622+
globalReleaseHandler && globalReleaseHandler()
617623
i18n.dispose()
618624
unmountApp()
619625
}
@@ -1650,7 +1656,12 @@ const globalExportMethods = !__LITE__
16501656
? ['t', 'rt', 'd', 'n', 'tm', 'te']
16511657
: ['t']
16521658

1653-
function injectGlobalFields(app: App, composer: Composer): void {
1659+
type GlobalInjectionReleaseHandler = () => void
1660+
1661+
function injectGlobalFields(
1662+
app: App,
1663+
composer: Composer
1664+
): GlobalInjectionReleaseHandler {
16541665
const i18n = Object.create(null)
16551666
globalExportProps.forEach(prop => {
16561667
const desc = Object.getOwnPropertyDescriptor(composer, prop)
@@ -1683,6 +1694,17 @@ function injectGlobalFields(app: App, composer: Composer): void {
16831694
}
16841695
Object.defineProperty(app.config.globalProperties, `$${method}`, desc)
16851696
})
1697+
1698+
const release = () => {
1699+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1700+
delete (app as any).config.globalProperties.$i18n
1701+
globalExportMethods.forEach(method => {
1702+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1703+
delete (app as any).config.globalProperties[`$${method}`]
1704+
})
1705+
}
1706+
1707+
return release
16861708
}
16871709

16881710
function injectGlobalFieldsForBridge(

0 commit comments

Comments
 (0)