Skip to content
Advertisement

Vue + Typescript – Import errors with class based decorators

I’m trying to set up Vue 3 with TypeScript and class-based components. However, I keep getting on error on importing the Component decorator the Vue constructor:

JavaScript

mycode.vue:

JavaScript

Advertisement

Answer

You might be trying to use the example from the official vue-class-component docs, but that’s currently for the 7x version, which can only be used with Vue 2.

Vue 3 requires vue-class-component 8x, which is not yet documented, but you can refer to vue-class-component Issue #406 that describes the changes. The notices relevant to your question:

  • @Component will be renamed to @Options.
  • @Options is optional if you don’t declare any options with it.
  • Vue constructor is provided from vue-class-component package.

Since your component has no options, you could just omit the @Options decorator from your component:

JavaScript

Also, Vue 3 no longer exports the Vue constructor, but vue-class-component does, so your component would have to extend that instead:

JavaScript

For reference, you can use Vue CLI to generate a Vue 3 + TypeScript project to play with a working example that uses the latest vue-class-component as described above.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement