elementui购物车界面
时间: 2023-07-19 13:07:38 浏览: 58
ElementUI提供了一些通用的组件,可以方便地构建购物车界面。下面是一个简单的ElementUI购物车界面示例:
```html
<template>
<div class="cart">
<el-table :data="cartData" style="width: 100%">
<el-table-column prop="name" label="商品名称"></el-table-column>
<el-table-column prop="price" label="单价"></el-table-column>
<el-table-column prop="quantity" label="数量">
<template slot-scope="scope">
<el-input-number v-model="scope.row.quantity" :min="1" @change="updateCart(scope.row)"></el-input-number>
</template>
</el-table-column>
<el-table-column label="小计">
<template slot-scope="scope">
{{ scope.row.price * scope.row.quantity }}
</template>
</el-table-column>
</el-table>
<div class="total">
<span>总计:</span>
<span>{{ total }}</span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
cartData: [
{ name: '商品1', price: 100, quantity: 1 },
{ name: '商品2', price: 200, quantity: 2 },
{ name: '商品3', price: 300, quantity: 3 }
]
}
},
computed: {
total() {
return this.cartData.reduce((sum, item) => sum + item.price * item.quantity, 0)
}
},
methods: {
updateCart(item) {
if (item.quantity < 1) {
item.quantity = 1
}
}
}
}
</script>
<style>
.cart {
margin: 20px;
}
.total {
margin-top: 20px;
text-align: right;
font-weight: bold;
}
</style>
```
上面的示例使用了ElementUI的`el-table`、`el-table-column`和`el-input-number`组件来显示购物车中的商品列表,并且计算出了购物车中所有商品的总价。你也可以根据自己的需求进行修改和扩展。