【发布时间】:2018-03-08 12:50:01
【问题描述】:
首先,这是我目前的结构
CHILD COMPONENT
// HTML
<v-select
v-bind:items="selectItems"
v-model="selectedItem"
label="Category"
item-value="text"
></v-select>
<v-text-field
label="Enter Value"
type="number"
v-model="compVal"
></v-text-field>
// JS
props: {
selectItems: {
type: Array,
required: true
},
selectedItem: {
type: String
},
compVal: {
type: Number
},
}
PARENT COMPONENT
// HTML
<component :selecItems="selectOneItems" :selectedItem="selectOneItem"
:compVal="compOneVal"></component>
<component :selecItems="selectTwoItems" :selectedItem="selectTwoItem"
:compVal="compTwoVal"></component>
// JS
data () {
return {
selectOneItems: [someArray],
selectedOneItem: null,
compOneVal: 0,
selectTwoItems: [someArray],
selectedTwoItem: null,
compTwoVal: 0
}
}
我的场景
1) 在初始状态,我必须从子文本输入中获取值并将其存储在本地。 (孩子对父母) 2)浏览器刷新后,我将检查任何本地数据,如果存在,我必须填充孩子的值(父母对孩子)
当前结构的错误
每当我输入或选择某些内容时都会触发此错误
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: "compOneValue"
那么如何避免这个错误呢?如何正确实现这个结构,以便我可以在任何我想要的地方重用这个组件。
【问题讨论】:
标签: vue.js vuejs2 vue-component