首页 > 解决方案 > 如何使用 v-for 标签显示数据中的元素?

问题描述

我正在创建 ec 模型,如何使用 v-for 标签

开发环境 Mac,Vue.js,Atom,Server 主机名为https://euas.person.ee/

Rigth Now↓<a href="https://i.stack.imgur.com/EYIHs.png" rel="nofollow noreferrer">在此处输入图像描述

我想要做的是为每一行显示订单 ID 和选项图像,例如

订单编号 | 订单说明 | 行动

1“OptionImage”详细信息按钮

在此处输入图像描述

订单列表Vue

<template>
<div class="OrderListing">
  <h2>My Orders</h2>
  <table class="table">
    <tr>
      <th>OrderId</th>
      <th>OrderDescription</th>
      <th>Action</th>
    </tr>

    <tr v-for="(cart, order) in this.orders" :key="order.id">
      <td>{{order}}</td>
      <td>{{cart}}</td>
      
      <td>
        <b-button variant="dark" :to=" '/orders/' + order">Detail</b-button>
      </td>
    </tr>

  </table>



</div>
</template>

<script>
import axios from "axios";
export default {
  name: 'OrderListing',
  props: {
    order: Object
  },
  data: function() {
    return {
      orders: []
    }
  },
  mounted() {
    axios.get("https://euas.person.ee/user/orders")
      .then(response => {
        this.orders = response.data;
      });
  }
}
</script>


<style scoped>
.option-image {
  max-height: 50px;
  max-width: 100px;
}
</style>

添加 在此处输入图像描述

ShoppingCartVue↓</p>

<template>
<div class="shopping-cart-page">
  <h2>ShoppingCart</h2>
  <table class="table">
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>qty</th>
      <th>Amount</th>
      <th>Actions</th>
    </tr>

    <tr v-for="(item, index) in this.items" :key="item.productId + '_' + index">
      <td>
        <img :src="item.optionImage" class="option-image" />
      </td>
      <td>{{ item.price }}</td>
      <td>{{ item.qty }}</td>
      <td>{{ item.total }}</td>
      <td>
        <b-button variant="danger" @click="removeItem(index)">Remove</b-button>
      </td>
    </tr>
    <tr class="total-row">
      <td>TOTAL:</td>
      <td></td>
      <td></td>
      <td>{{ total }}</td>
      <td></td>
    </tr>
  </table>

  <b-button variant="success" size="lg" @click="orderNow" v-if="this.items.length">Order Now!</b-button>

</div>
</template>

<script>
import axios from "axios";
export default {
  name: 'ShoppingCartPage',
  computed: {
    items: function() {
      return this.$root.$data.cart.items || [];
    },
    total: function() {
      let sum = 0
      for (const item of this.items) {
        sum += item.total
      }
      return sum
    }
  },
  methods: {
    removeItem: function(index) {
      if (!this.$root.$data.cart.items) this.$root.$data.cart.items = []
      this.$root.$data.cart.items.splice(index, 1);
      console.log(this.$root.$data.cart.items);
      this.$root.$data.saveCart();
    },

    orderNow: function() {
      let data = this.$root.$data

      axios.post("https://euas.person.ee/user/carts/" + this.$root.$data.cart.id + "/orders/",
        this.$root.$data.cart).then(function() {
        data.reinitCart();
      })
    }
  }
}
</script>


<style scoped>
.option-image {
  max-height: 50px;
  max-width: 100px;
}
</style>

在此处输入图像描述

标签: restvue.jsweb

解决方案


据我了解,您正在寻找这样的东西:

<div v-for="order in orders" :key="order.id">
     <div v-for="item in order.items" :key="item.productId">
         <img :src="item.optionImage" class="option-image" />
     </div>
 </div>

推荐阅读