我有很多窗體可以在我正在使用的Web應用程序中創建,爲此我使用Vue,所以我一直試圖創建一個可以使用的通用輸入組件始終。我使用的是Bootstrap網格,所以我的想法是,我應該能夠傳遞組件的許多列來佔用,標籤,名稱和屬性用作v模型。我覺得,我有點不習慣,但是我遇到了一個突變道具的問題 - [Vue warn]:避免直接改變道具,因爲只要父組件重新渲染,值就會被覆蓋。相反,使用基於道具值的數據或計算屬性。支柱正在發生變異:「型號」 (在組件中找到)。 這裏是模板(以簡化形式):試圖創建通用輸入組件
<template id="field">
<div v-bind:class="colsClass">
<div class='form-group form-group-sm'>
<label v-bind:for="name">{{labelText}}</label>
<input v-bind:id='name' ref="input" class='form-control' v-bind:name='name' v-model='model'/>
</div>
</div>
而這裏的(再次簡化的)JS:
Vue.component('field', {
template: '#field',
props: ['cols','label','group','name','model'],
computed:{
colsClass:function(){
return "col-xs-"+this.cols
}
,
labelText:function(){
if(this.label) {
return this.label
} else {
return _.startCase(this.name);
}
}
}
});
這從另一個 '編輯產品' 組件中使用,像這樣:
<field :cols="8" name="name" :model="product.name"></field>
這顯示確定,但拋出錯誤(或更多精確度伊利,警告),當我編輯字段的值。那麼我做錯了什麼?