5

I am trying to create a vue component with vue-class-component and typescript (found here https://github.com/vuejs/vue-class-component). From what I understand, data is defined in the class, as I have done below - yet I receive the error:

"[Vue warn]: Property or method "test" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property."

Here is a stripped down version of my actual code, but it still doesn't work:

<template>

  <div id="vue-test">
    <input v-model="test"/>
    {{test}}
  </div>

</template>

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

@Component({
})
export default class AppearanceDialogVue extends Vue {
  public test: string = '100';
}

</script>

EDIT: It appears that changing 'test's declaration to

public test: string;

worked

3
  • Did you try test = '100'; without public and string? Commented Jul 2, 2018 at 16:11
  • Your code runs for me without error/warning. Commented Jul 2, 2018 at 16:59
  • I have the same issue, I tried every combination for the test declaration, but I get the error no matter what I do. Commented Oct 14, 2018 at 4:32

1 Answer 1

4

Here is the solution to this issue, need to add a constructor and initialize the property in the constructor

<template>
<section class="dropdown" style="background-color: #dde0e3">
  <select class="btnList" v-model="foo">
    <option v-for="item in selectedfooData" :value="item" :key="item.id">{{item}}</option>
    </select>
    {{foo}}
  </section>
</template>

<script lang="ts">
  import { Component, Prop, Vue } from 'vue-property-decorator';
  @Component
  export default class HelloWorld extends Vue {  
  private foo: string;
  private selectedfooData : string[] = [
   'one',
   'two'
  ]
  construtor() { 
    super();
    this.foo = '';
  }

 }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
Sorry, have found the solution to this issue, edited the answer @JobinJoseph

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.