Challenges with importing class-based decorators in Vue using Typescript

Currently, I am in the process of setting up Vue 3 with TypeScript and utilizing class-based components. Unfortunately, I have encountered an issue while attempting to import the Component decorator within the Vue constructor:

An error message appears stating: '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 // Error #1 '@Component'
export default class ProdItem extends Vue { // Error #2 'Vue'
  
}
</script>

Answer №1

If you're following the example in the official vue-class-component documentation, keep in mind that it pertains to version 7x and is only compatible with Vue 2.

To work with Vue 3, you'll need to use vue-class-component version 8x. While there isn't documented information yet, you can check out vue-class-component Issue #406 for updates highlighting key changes:

  • @Component will be replaced by @Options.
  • @Options becomes optional if no options are declared with it.
  • The Vue constructor is now provided by the vue-class-component package.

If your component doesn't have any options, you can skip using the @Options decorator altogether:

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

// AFTER:
/* since no options are used, @Options decorator is not needed */
class {}

Additionally, Vue 3 no longer exports the Vue constructor directly, so you should extend it from vue-class-component instead:

// BEFORE:
import Vue from 'vue'

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

To get a hands-on experience with the latest vue-class-component setup as described above, consider using Vue CLI to generate a project with Vue 3 + TypeScript.

Answer №2

Eliminate the need for ({}) with a decorator. Give this code a shot:

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

@Component  // Issue here: '@Component'
export default class ProdItem extends Vue { // Problem here: 'Vue'
  
}
</script>

Answer №3

Here's a solution for the problem presented in both this issue and this one:

<script lang="ts">

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


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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with create-react-app and express server not displaying correctly in Internet Explorer

My application functions perfectly with Chrome and Safari. It utilizes React for the front-end and Express for the back-end. However, when I try to open it in Internet Explorer, all I see is a blank white page. Additionally, I encounter this error message: ...

Transforming jQuery code to pure vanilla Javascript

For my project, I decided to convert my jQuery code into native JavaScript. The goal is to eliminate the dependency on jQuery and achieve the same functionality. The current setup involves two pages - page.html and side.html. The page.html document fetches ...

"Efficiently handle JSON and binary data passing with the enhanced functionality of Express Body Parser

Within my express app router, I have routes set up to handle both POST requests with JSON data and binary data. The issue arises when I use body parser to parse the JSON data, as it incorrectly interprets the binary data as JSON and causes errors during th ...

Is there a way to make all Bootstrap column heights equal using only jQuery?

I am currently working on matching the heights of two columns without using an existing library like match-height.js. In this specific case, I have a row with 2 columns where the first column contains a centered black square and the second column contains ...

Vue JS - Issue with rendering <tr> and <td> elements in table component

I have created a custom table component using vue.js: <template> <div> <table class="table border"> <thead> <tr> <td>header 1&l ...

Exploring solutions for handling asynchronous issues with vue3-google-map

While working with a Vue library for managing Maps called vue3-google-map, I encountered an issue when trying to define certain polylines that would not allow me to select the center of the marked area: Here is my map template: <template> <Goo ...

The attribute 'status' is not found in the 'ServerResponse' type (TS2339)

I've attempted to develop an interface and install React types, but the only way it seems to work is when I write the code in JavaScript. However, within a TypeScript project, I encounter this error: Property 'status' does not exist on typ ...

Unable to save the newly generated data into MongoDB

I'm currently working on a small project where users can generate random hex colors and save them to a database. However, I seem to be encountering an issue as the data is not being saved to the database. My index.js file serves as the main file for d ...

Can you identify the issue with the phase control in my sine wave program?

I have recently developed an interactive sine wave drawing web application. Below is the code snippet for reference: const canvas = document.getElementById("canvas"), amplitude_slider = document.getElementById("amplitude_control"), wavelength_slider ...

Check if an element possesses a specific property and corresponding value in JavaScript

I was looking to determine if an object has a specific property with a certain value, and wanted to search for it within an array of objects. var test = [{name : "joey", age: 15}, {name: "hell", age: 12}] After testing the code snippet below, I realized ...

Click on a div in AngularJS to be directed to a specific URL

I'm currently working on developing an Angular mobile app and I want to be able to navigate to a specific URL, like www.google.com, when a particular div is clicked. Unfortunately, I'm still new to the world of Angular and struggling to achieve t ...

A guide to accessing parent attributes in Vue3 using typescript

Within my child component, I am facing an issue where I need to access the parent object but the commented lines are not functioning as expected. The structure of AccordionState is defined below: export type AccordionKeys = | "open" | "disa ...

Challenge with Dependency Injection in the Handlers of NestJS CQRS repositories

As a newcomer to nodejs, I am currently delving into the implementation of NestJS's CQRS 'recipe'. In my service, I have a Request scoped with the injection of QueryBus: @Injectable({scope: Scope.REQUEST}) export class CustomerService { co ...

Bug causing horizontal lines to glitch

In my Voxel.js project, which is based on Three.js, I have noticed a strange rendering issue specifically on Macbook Airs with Intel HD Graphics 3000. Interestingly, this problem does not occur on other Macbooks like those with GeForce 320Ms. I have gathe ...

Overlay a translucent image on top of another using JavaScript

Is it feasible to overlay a transparent-background image onto another image using JavaScript? For example, if you have a website featuring various product pictures and some of these products are recalled, can you superimpose the Do-Not-Enter sign (circle-w ...

Filtering text for highlighting in Vue.js is a breeze

Struggling to create a text highlight filter using vuejs. The task involves iterating through an array of words, highlighting any matches with a span and class. However, I'm facing difficulty in getting the data to return with proper HTML formatting i ...

Validation in Ajax Response

When receiving an object from my API call, I want to perform error checking before displaying the JSON data in my view. function response(oResponse) { if (oResponse && oResponse != null && typeof oResponse === "object" && oResponse.response ...

Clicking on the checkbox will trigger the corresponding table column to disappear

Upon clicking the filter icon in the top right corner, a menu will open. Within that menu, there are table header values with checkboxes. When a checkbox for a specific value is selected, the corresponding table column should be hidden. I have already impl ...

Oops! Looks like there was an issue with defining Angular in AngularJS

I am encountering issues while attempting to launch my Angular application. When using npm install, I encountered the following error: ReferenceError: angular is not defined at Object.<anonymous> (C:\Users\GrupoBECM18\Documents&bs ...

What is the process of creating a TypeScript interface that accommodates various schemas?

Trying to establish a Consume type with two different payloads. interface Eat { category: 'meat' | 'vegetable' } interface Drink { size: string } interface Consume { type: 'eat' | 'drink' payload: Eat | Dri ...