0

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

1 Answer 1

2

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 {}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that solution is correct however what worked for me was including the above code in my log.ts file instead of splitting it into a .d.ts file. Same as this guy stackoverflow.com/a/54656902/8518207
If you add a .d.ts file, make sure it's in the source path configured in tsconfig.json for tsc (and Intellisense) to find it. Putting it in src/ works for Vue CLI projects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.