在下面的代码中,我有一个值,这是一个对象,如何将其推送到现有数组。
methods: {
onChange(event) {
this.newItems.push(event.target.value);
console.log(event.target.value);
}
}
我的 Blade 是:
<select @change='onChange($event)' class="form-control">
<option value="" selected disabled>Choose</option>
<option v-for='item,key in items' :value="item">@{{ item.name }}</option>
</select>
最佳答案
我建议在选择上使用 v-model 来检测当前选择的内容。
<div id="app">
<select @change="onChange" class="form-control" v-model="selected">
<option value="" selected disabled>Choose</option>
<option v-for='item,key in items' :value="item">@{{ item.name }}
</option>
</select>
<p v-for="newItem in newItems">
{{newItem}}
</p>
</div>
然后将 this.selected 推送到 onChange 方法中:
this.newItems.push(this.selected)
希望这有帮助。
对代码进行一些小的修改即可工作:
关于javascript - 如何将对象推送到 Vue.js 中的现有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247216/