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