1

I'm trying to use Vue with typescript but am getting an error, saying that my member variable is undefined.

import { Component, Vue, Prop } from 'vue-property-decorator';
import UploadApi from './UploadApi';
import ApiConfig from '../../api/api.config';

@Component
export default class UploadCard extends Vue {
  uploading = false;

  private uploads: Array<Upload> = [];

  private api: UploadApi = new UploadApi(ApiConfig.get());

  constructor() {
    super();
    this.getUploads();
  }

  private getUploads() {
    // const loading = this.$store.state.vs.loading();
    // mimic http request
    console.log(this.$data);
    console.log(this.uploading);
    this.api.getUploads().then((response) => {
      console.log(response);
    });

Here for example it is saying this.api is undefined, does anyone know about this?

2
  • Hi! I’m not sure about your question - do you mind sharing for code, or perhaps what your UploadApimofule looks like? We’ll be able to help you better :D Commented Aug 28, 2020 at 14:08
  • The lifecycle model worked, using created instead of constructor. Commented Aug 28, 2020 at 14:13

1 Answer 1

1

Rather than using a constructor, use the lifecycle methods to do setup tasks. For example, replace your constructor with created or mounted:

created() {
    this.getUploads();
}

Please see the docs for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so, the class here is a bit of red-herring? you ignore the constructor but use the lifecycle events.
Yes, I added a link to the docs: class-component.vuejs.org/guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.