我使用 Vue 和 Bootstrap Tokenfield 创建动态组件。但v-model
在这种情况下不起作用。
假设,我在下面有一个数组:
索引variant_options
1 aaa
2 sss
当我删除索引 1 时,索引 1 的结果应该是“sss”但仍然是“aaa”
<div class="card" v-for="(variant, index) in form.variants" :key="index">
<div class="card-body"> <span class="float-right" style="cursor: pointer" @click="deleteVariant(index)">
X
</span>
<div class="row">
<div class="col-md-4">
<label for="weight">Variant Type {{ index + 1 }} </label>
<div class="input-group">
<input type="text" id="variant_type" class="form-control" v-model="
variant.variant_type
" @keyup="tokenField()" placeholder="Input variant type. E.g: Color" name="name" required autofocus /> </div>
</div>
<div class="col-md-8">
<label for="weight">Variant Options {{ index + 1 }}</label>
<div class="input-group">
<input type="text" id="variant_options" autofocus="true" v-model="
variant.variant_options
" @mouseover="
tokenField()
" placeholder="Input variant options. E.g: Blue, Brown," class="
form-control
variant_options
" /> </div>
data() {
return {
form: new Form({
variants: [
{
variant_type: '',
variant_options: '',
},
],
}),
};
},
methods: {
tokenField() {
$('.variant_options').tokenfield({
showAutocompleteOnFocus: true,
});
},
addVariant() {
if (this.form.variants.length <= 1) {
this.form.variants.push({
variant_type: '',
variant_options: '',
});
} else {
this.error = 'You can only add 2 type of varians';
$('#errMsg').show();
}
},
deleteVariant(index) {
this.form.variants.splice(index, 1);
$('#errMsg').hide();
},
}, // methods: