Little Monkey
Little Monkey

Reputation: 6157

Error "TS2339: Property 'X' does not exist on type 'Y'" when calling a data property in a method

I'm using Vuejs 3 with Typescript (and I'm new to all of it).

When I push a button, I want to get the tasks for a "to do" list.

This is my script:

<script lang="ts">
import axios from 'axios';

export default {
  name: 'Todos',

  data() {
    return {
      tasks: Array<Task>(),
    }
  },
  methods: {
    async getTasks() {
        await axios.get('https://.../tasks').then(response => {
           this.tasks = response.data 
        })
  }
}

Simply writing this.tasks gives me this error:

TS2339: Property 'tasks' does not exist on type '{ getTasks(): Promise ; }'.

How can I call a data property from a method?

Upvotes: 0

Views: 456

Answers (1)

Michal Lev&#253;
Michal Lev&#253;

Reputation: 37753

When using TS, you need to define components like this:

import { defineComponent } from 'vue'

export default defineComponent({
    ...
})

Documentation

Upvotes: 1

Related Questions