Tìm kiếm trang web

typescript - Cảnh báo thành phần dựa trên lớp Vue: Thuộc tính không được định nghĩa trên thể hiện nhưng lại được tham chiếu trong quá trình render


Tôi đang cố gắng tạo một component Vue với vue-class-component và TypeScript (có thể tìm thấy tại đây https://github.com/vuejs/vue-class-component). Theo như tôi hiểu, dữ liệu được định nghĩa trong lớp, như tôi đã làm bên dưới - tuy nhiên tôi nhận được lỗi:

"[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."

Đây là phiên bản rút gọn của mã thực tế của tôi, nhưng nó vẫn không hoạt động:

<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: Có vẻ như việc thay đổi khai báo 'test' thành

public test: string;

đã làm việc

Câu trả lời #1

Đây là giải pháp cho vấn đề này, cần thêm một hàm khởi tạo và khởi tạo thuộc tính trong hàm khởi tạo

<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>

Bài viết liên quan