Vue: ошибка TypeScript при использовании Vue.mixin

Я создаю приложение SSR, используя Vue.js.

Я столкнулся с ошибкой машинописи, когда пытался это.

Vue.mixin({
    beforeRouteUpdate (to, from, next) {
        const { asyncData } = this.$options
        if (asyncData) {
            asyncData({
                store: this.$store,
                route: to
            }).then(next).catch(next)
        } else {
            next()
        }
    }
})

И это ошибка.

Property '$options' does not exist on type 'VueConstructor<Vue> | ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.


Property '$store' does not exist on type 'VueConstructor<Vue> | ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.

Как я могу избежать этой ошибки? Я новичок в машинописи. Спасибо.


person R. M.    schedule 22.12.2018    source источник
comment
Нашел решение (нехорошее, но работает) =› (это как любое).$store   -  person R. M.    schedule 30.12.2018


Ответы (1)


Вы можете сделать что-то вроде этого:

Vue.mixin({
    beforeRouteUpdate ((this as Vue), to, from, next) {
        const vm = this as Vue;
        const { asyncData } = vm.$options
        if (asyncData) {
            asyncData({
                store: vm.$store,
                route: to
            }).then(next).catch(next)
        } else {
            next()
        }
    }
})

person Steve Maris    schedule 24.01.2019