【发布时间】:2020-03-28 12:54:49
【问题描述】:
我看到很多关于 Vue + Typescript 组件类的相互冲突的文档。
在哪里定义属性?在@Component 注释中概述的here?正如@Prop 注释的实例属性所概述的here?
在哪里初始化定义的属性?在构造函数中?在字段级属性定义中?
是否有关于这些内容的明确、最新的参考,或最新的示例应用程序?
这是我现在拥有的:
<template>
<div class='contacts'>
<b-form @submit="search">
<b-form-group>
<b-form-input v-model="q"></b-form-input>
</b-form-group>
<b-button type="submit" variant="primary">Search</b-button>
</b-form>
<b-table :items='contacts'></b-table>
</div>
</template>
<script lang="ts">
import {Component, Prop, Vue} from 'vue-property-decorator'
@Component
export default class Contacts extends Vue {
constructor(options: any) {
super(options);
this.q = 'dummy data';
this.contacts = [{
'id': 1,
'first_name': 'Lukas',
'last_name': 'Stigers',
'email': null,
'gender': 'Male',
'phone': '776-878-7222'
}, {
'id': 2,
'first_name': 'Dari',
'last_name': 'Matkin',
'email': null,
'gender': 'Female',
'phone': '833-146-3305'
}]
}
@Prop() private q: string
@Prop() private contacts: any
search(event:Event) {
event.preventDefault();
alert('You searched for ' + this.q)
}
}
</script>
这可行,但我在浏览器中收到以下警告:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "q"
【问题讨论】:
标签: typescript vue.js vuejs2 vue-component