vue v-for及對數據的 push 操作

<template>
  <div class="hello">
    <div class="top">這裏是首頁</div>
    <button @click="addItem">增加</button>
    <ul>
      <li v-for="g in list">
        <span>{{g.mm}}</span>**
        <span>{{g.price}}</span>
      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    name: 'hello',
    data () {
      return {
        name: 'Welcome to Your Vue.js App',
        list: [
          {mm: '路飛', price: 50},
          {mm: '娜美', price: 20},
          {mm: '喬巴', price: 5},
          {mm: '索隆', price: 45}
        ]
      }
    },
    methods: {
      addItem () {
        // 這裏對數據進行操作
        this.list.push({mm: '白鬍子', price: 80})
      }
    }
  }</script>
<style scoped>h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  margin: 0 10px;
}

a {
  color: #42b983;
}

.top {
  margin: auto;
  width: 200px;
  height: 100px;
  background: #f56868;
}</style>

效果如圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章