【问题标题】:How can I solve "Duplicated key"?如何解决“重复密钥”?
【发布时间】:2020-07-18 12:49:29
【问题描述】:

npm 运行后服务↓

52:7 错误 Duplicated key 'order' vue/no-dupe-keys

我现在只尝试说明 {order.id},但这是第一次使用 Vue.js,因此我不知道如何解决它。 另外,我的最终成就是每列都有 {{item.image}},{{item.price}},{{item.qty}},{{item.amount}}。

OverDetailVue

<template>
<div class="OrderListing">
  <h2>Order Detail</h2>
  <table class="table">
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>qty</th>
      <th>Amount</th>
    </tr>

    <tr v-for="order in this.orders" :key="order.id">
      <td>{{ order.price }}</td>
      <td>{{ item.qty }}</td>
      <td>{{ item.total }}</td>
    </tr>
  </table>

  <tr class="total-row">
    <td>TOTAL:</td>
    <td></td>
    <td></td>
    <td>{{ total }}</td>
    <td></td>
  </tr>


</div>
</template>

<script>
import axios from "axios";
export default {
  name: 'OrderListing',
  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
    }
  },
  props: {
    order: Object
  },
  data: function() {
    return {
      order: {}
    }
  },
  mounted() {
    axios
      .get("https://euas.person.ee/orders/" + this.$route.params.orderId)
      .then(response => {
        this.orders = response.data;
      }).catch(error => {
        console.log(error);
      })
  }
}
</script>


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

【问题讨论】:

  • orders 检查是否有任何order id 重复。 id 在这里需要区分,不能重复。
  • order 在数据和模板中定义
  • 那么,你的意思是我应该删除模板顺序还是数据顺序?
  • 是的,只需选择一个并使用不同的名称。现在,如果您在v-for 中使用order,模板无法知道您是指来自数据属性的order 还是来自v-fororder。也许你认为你必须提前声明循环变量,但你没有(不能)。也不要在模板中使用this.orders,只需使用orders。哦,你还有一个名为 order 的道具。那是你声明了同一个变量的 3 个地方,所以会有冲突。不知道你要去那里,但这不起作用。
  • 非常感谢,正如你所说,我把订单和订单搞混了。我更改了一些代码。

标签: node.js vue.js web


【解决方案1】:

<template>
<div class="OrderListing">
  <h2>Order Detail</h2>
  <table class="table">
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>qty</th>
      <th>Amount</th>
    </tr>

    <tr v-for="(item, index) in this.order.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>
    </tr>
    <tr class="total-row">
      <td>TOTAL:</td>
      <td></td>
      <td></td>
      <td>{{ total }}</td>
    </tr>
  </table>



</div>
</template>

<script>
import axios from "axios";
export default {
  name: 'OrderListing',
  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
    }
  },
  data: function() {
    return {
      order:{}
    }
  },
  mounted() {
    axios
      .get("https://euas.person.ee/user/orders/" + this.$route.params.orderId)
      .then(response => {
        this.order = response.data;
      }).catch(error => {
        console.log(error);
      })
  }
}
</script>


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多