Vue Class Based Component Warning: Property is not defined on the instance but referenced during render(Vue 基于类的组件警告:属性未在实例上定义,但在渲染期间引用)
问题描述
我正在尝试使用 vue-class-component 和 typescript 创建一个 vue 组件(在此处找到 https://github.com/vuejs/vue-class-component).据我了解,数据是在类中定义的,如下所示 - 但我收到错误:
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>
似乎将测试"的声明更改为
It appears that changing 'test's declaration to
public test: string;
工作
推荐答案
这里是解决这个问题的方法,需要添加一个构造函数,并在构造函数中初始化属性
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>
这篇关于Vue 基于类的组件警告:属性未在实例上定义,但在渲染期间引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vue 基于类的组件警告:属性未在实例上定义,但在渲染期间引用
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01