Vuejs-如何将新值添加到itemList数组

时间:2019-12-28 03:36:19

标签: vue.js

我是Vuejs的新手,请帮助我-如何将新值添加到itemList数组

    new Vue({
    el: "#app",
    data: {
        rows: [],
        itemList: [
            { code: 'Select an Item', description: '', unitprice: ''},
            { code: 'One', description: 'Item A', unitprice: '10'},
            { code: 'Two', description: 'Item B', unitprice: '22'},
            { code: 'Three', description: 'Item C', unitprice: '56'}
        ]
    }
 });

1 个答案:

答案 0 :(得分:2)

new Vue({
    el: "#app",
    data: {
        rows: [],
        itemList: [
            { code: 'Select an Item', description: '', unitprice: ''},
            { code: 'One', description: 'Item A', unitprice: '10'},
            { code: 'Two', description: 'Item B', unitprice: '22'},
            { code: 'Three', description: 'Item C', unitprice: '56'}
        ]
    },
    methods:{
      addItem(){
         this.itemList.push({code:'Four',description:'Item D',unitprice:'0'});
      }
    }
 });

您可以使用模板或javascript调用addItem。