'Vue typescript plugin

I am writing a vue logger plugin

export default new (class NestLogger {
  public install(Vue: any) {
    Vue.prototype.$log = this;
  }

  error(text: string) {
      console.log(text)
  }
})();

in main.ts

import logger from "./plugins/logger";
Vue.use(logger);

But in components I cannot reference this.$log - why?

In Login.vue

this.$log.error("bla"); 

Error: Property $log does not exist on type Login



Solution 1:[1]

You need to augment the vue type definitions in a .d.ts file to include the type definition for $log:

// src/my-log-plugin.d.ts
import Vue from 'vue'

declare module 'vue/types/vue' {
  interface Vue {
    $log: {
      error: (message: string) => void
    }
  }
}

export {}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1