1

I have a Vue mixin like so:

const mixin = Vue.extend({
    methods: {
        $languages: function(): object {
            return {
                en: 'english'
            }
        }
    }
}


Vue.mixin(mixin)
new Vue({
    router,
    render: h => h(Frame)
}).$mount('#app')

... and I'm trying to use it in a component:

<template lang="pug">
    .languages
        a( v-for="language, code in $languages()" :key="code" @click="$root.$i18n.locale = code") {{ language }}&nbsp;
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
    methods: {
        languages: function () {
            console.log(this.$languages)
        }
    }
})
</script>

But I get an error, saying "Property '$languages' does not exist on type 'CombinedVueInstance void; }, unknown, Readonly>>'"

Curiously, if I'm just trying to use it in the template, it works. The language name appears. It's just the typescript code that doesn't recognise the function.

1 Answer 1

1

You need to augment the vue module to provide a typing for $languages, observe:

// vue-shim.d.ts

declare module 'vue/types/vue' {
  interface Vue {
    $languages: LanguageService
  }
}

And a psuedo LanguageService

// language-service.d.ts

export interface LanguageService {
  $languages: () => Record<string, string>
}

You can read more about augmenting Vue here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much @Ohgodwhy. Turns out, it was enough to chuck in the module declaration with '$languages: object' in the same file with my mixin and it starting working.
Correction: $languages: () => {[key: string]: string}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.