K
K
Kotaro Hiba2021-02-24 19:20:44
typescript
Kotaro Hiba, 2021-02-24 19:20:44

How to make your own plugin for Vue2 if the project is in TypeScript?

Hello, I'm trying TS + Vue decided to try to make a global method for Vue.

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false
const Plugin = {
  install(Vue: any, options?: any) {
    Vue.prototype.$Kota = function () {
      console.log('Мяу')
    }
  }
}
Vue.use(Plugin)

new Vue({
  router,
  store,
  render: h => h(App),

}).$mount('#app')


Here is the plugin itself, when trying to use it in another IDE component, it gives an error TS2339
: Property '$Kota' does not exist on type 'CombinedVueInstance >>'
I'm doing it wrong, all the configs are standard which Vue cli sets when creating a project

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2021-02-24
@lina666

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

declare module 'vue/types/vue' {
  interface Vue {
    $Kota: () => void
  }
}

const Plugin = {
  install(Vue: any, options?: any) {
    Vue.prototype.$Kota = function () {
      console.log('Мяу')
    }
  }
}

Vue.use(Plugin)

new Vue({
  router,
  store,
  render: h => h(App),

}).$mount('#app')

D
Dmitry Belyaev, 2021-02-24
@bingo347

https://www.typescriptlang.org/docs/handbook/decla...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question