[Vuejs]-Vue 3 composition API – creating custom plugin with Typescript type augmenting

1πŸ‘

βœ…

It’s necessary to import $translation in script section any way, global variables (not properties) can’t appear out of nowhere, unless some kind of magic transforms like Unplugin are used. In order to do that, it should be exported:

export let $translation: (key: string) => string;

export const plugin = {
    install(app: App, options: any) {
        $translation = (key: string) =>  {...};
        app.config.globalProperties.$translation = $translation
    }
}

For Vue 3, @vue/runtime-core type augmentation can be tried instead of vue:

declare module '@vue/runtime-core' {
    interface ComponentCustomProperties {
        $translation: (key: string) => string;
    }
}
πŸ‘€Estus Flask

Leave a comment