Skip to content

Commit 9d0b935

Browse files
authored
fix: cannot hook at beforeUnmount/unmounted (#524)
closes #523
1 parent 336f7ff commit 9d0b935

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

packages/vue-i18n/src/mixin.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentInstance } from 'vue'
1+
import { getCurrentInstance, nextTick } from 'vue'
22
import { getLocaleMessages, SetPluralRulesSymbol } from './composer'
33
import { createVueI18n } from './legacy'
44
import { createI18nError, I18nErrorCodes } from './errors'
@@ -100,34 +100,36 @@ export function defineMixin(
100100
}
101101
},
102102

103-
beforeUnmount(): void {
103+
unmounted(): void {
104104
const instance = getCurrentInstance()
105105
/* istanbul ignore if */
106106
if (!instance) {
107107
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
108108
}
109109

110-
/* istanbul ignore if */
111-
if ((__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) && !__NODE_JS__) {
112-
if (this.__v_emitter) {
113-
this.__v_emitter.off('*', addTimelineEvent)
114-
delete this.__v_emitter
110+
nextTick(() => {
111+
/* istanbul ignore if */
112+
if ((__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) && !__NODE_JS__) {
113+
if (this.__v_emitter) {
114+
this.__v_emitter.off('*', addTimelineEvent)
115+
delete this.__v_emitter
116+
}
117+
const _vueI18n = (this.$i18n as unknown) as VueI18nInternal
118+
_vueI18n.__disableEmitter && _vueI18n.__disableEmitter()
119+
delete this.$el.__VUE_I18N__
115120
}
116-
const _vueI18n = (this.$i18n as unknown) as VueI18nInternal
117-
_vueI18n.__disableEmitter && _vueI18n.__disableEmitter()
118-
delete this.$el.__VUE_I18N__
119-
}
120121

121-
delete this.$t
122-
delete this.$rt
123-
delete this.$tc
124-
delete this.$te
125-
delete this.$d
126-
delete this.$n
127-
delete this.$tm
122+
delete this.$t
123+
delete this.$rt
124+
delete this.$tc
125+
delete this.$te
126+
delete this.$d
127+
delete this.$n
128+
delete this.$tm
128129

129-
i18n.__deleteInstance(instance)
130-
delete this.$i18n
130+
i18n.__deleteInstance(instance)
131+
delete this.$i18n
132+
})
131133
}
132134
}
133135
}

packages/vue-i18n/test/i18n.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,19 @@ describe('slot reactivity', () => {
424424
<i18n-t keypath='hello'/>
425425
</SlotChild>
426426
</div>
427-
`
427+
`,
428+
created() {
429+
// @ts-ignore
430+
console.log('child created $t', this.$t)
431+
},
432+
beforeUnmount() {
433+
// @ts-ignore
434+
console.log('child beforeUnmount $t', this.$t)
435+
},
436+
unmounted() {
437+
// @ts-ignore
438+
console.log('child unmounted $t', this.$t)
439+
}
428440
}
429441

430442
const App = defineComponent({

0 commit comments

Comments
 (0)