Vue + Typescript - Import errors with class based decorators

十年热恋 提交于 2021-01-29 07:37:33

问题


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:

This expression is not callable. Type 'typeof
import("/Users/*folder*/node_modules/vue-class-component/dist/vue-class-component")'
has no call signatures. Vetur(2349)

mycode.vue:

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component // 1st Error '@Component'
export default class ProdItem extends Vue { // 2nd error 'Vue'
  
}
</script>

回答1:


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:

// BEFORE:
import Component from 'vue-class-component'
@Component
class {}

// AFTER:
/* no options used, so no @Options decorator needed */
class {}

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

// BEFORE:
import Vue from 'vue'

// AFTER:
import { Vue } from 'vue-class-component'

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.




回答2:


Based on this issue and this one you could do :

<script lang="ts">

import {vue} from 'vue-class-component'


export default class ProdItem extends Vue { 
  
}
</script>



回答3:


With a decorator, you don't need ({}). Try

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component  // 1st Error '@Component'
export default class ProdItem extends Vue { // 2nd error 'Vue'
  
}
</script>


来源:https://stackoverflow.com/questions/64262860/vue-typescript-import-errors-with-class-based-decorators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!